Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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,
C#
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
Updated 11-Jul-13 22:21pm
v2
Comments
berrymaria 12-Jul-13 5:24am    
Try to see this link:
http://stackoverflow.com/questions/4645704/c-sharp-display-images-randomly-and-one-after-another

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
C#
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.
 
Share this answer
 
try to add one jquery plugin, i think its an easy way
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900