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;
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.