Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
I have windows application in c#. It contain one Picture box and two buttons(button names are Next, Previous). i need to display 10 images in picture box when i clicked the buttons.
for eg, i clicked the previous button it will display previous image in picture box, and i click the next button it display the next image.
 
i have tried the following codings,
private void Next_Click(object sender, EventArgs e)
       {
          int  i = 0;
           string filePath = "C:/Users/Pictures/2013-06-10";
           string[] files = Directory.GetFiles(filePath);
           i++;
           if (i >= 11)
           {
               pictureBox1.Image = null;
           }
           else
           {
               pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
               pictureBox1.Image = Image.FromFile(files[i]);
           }
       }
 
       private void Previous_Click(object sender, EventArgs e)
       {
           int j = 10;
           string filePath = "C:/Users/Pictures/2013-06-10";
           string[] files = Directory.GetFiles(filePath);
           j--;
           if (j <= 0 || j == 10)
           {
               pictureBox1.Image = null;
           }
           else
           {
               pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
               pictureBox1.Image = Image.FromFile(files[j]);
           }
       }
Posted 11-Jul-13 23:20pm
Edited 11-Jul-13 23:21pm
v2
Comments
chimcham at 12-Jul-13 5:24am
   
Try to see this link:
http://stackoverflow.com/questions/4645704/c-sharp-display-images-randomly-and-one-after-another
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The problem here is that you create an index variable i on every button click. It gets initialized on every button click with the very same value. What you need is a member variable that belongs to the object that contains the methods Next_Click() and Previous_Click. Something like this
System.Windows.Form Form1
{
    private int _pictureIndex = 0;
 
    // Next line can be set in Designer. No need to code it.
    //pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
    
    private void Next_Click(object sender, EventArgs e)
    {
        string filePath = "C:/Users/Pictures/2013-06-10";
        string[] files = Directory.GetFiles(filePath);
        _pictureIndex++;
        if (_pictureIndex > files.Length)
        {
            _pictureIndex = 0;
        }
        pictureBox1.Image = Image.FromFile(files[_pictureIndex]);
    }
}
Of course, the hard-coded file path is still ugly. But I guess, that's for demonstration purposes only.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

try to add one jquery plugin, i think its an easy way
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 584
1 Maciej Los 275
2 DamithSL 233
3 Sergey Alexandrovich Kryukov 209
4 BillWoodruff 200
0 OriginalGriff 6,803
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,421
3 Manas Bhardwaj 4,841
4 Maciej Los 4,330


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 12 Jul 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100