Q:

Display image in picturebox one after another

I am trying to make a simple app that grabs all *.jpg files from a folder, and displays them in PictureBox.
However i'm trying to make Prev/Next buttons to show images one by one. Here's my code for the Next button so far:
string path = @"..\..\Resources\Wallpapers\";
int count = 0;

private void Form1_Load(object sender, EventArgs e)
{
    DisplayNextFile(count);
}

private void next_Click(object sender, EventArgs e)
{
    DisplayNextFile(count);
}

private void DisplayNextFile(int c)
{
    var rand = new Random();
    var files = Directory.GetFiles(path, "*.jpg");
    var images = new Image[files.Length];

    for (int i = c; i < files.Length; ++i)
    {
        images[i] = Image.FromFile(files[i]);
        picBoxMainPreview.Image = images[i];
        break;
    }

    count++;

    if (count == files.Length)
        count = 0;
}

It works fine, but how can I do it for the Previous button?

A:

Remove count++; from DisplayNextFile.
private void next_Click(object sender, EventArgs e)
{
    count = (++count) % files.Length;
    DisplayNextFile(count);
}

private void previous_Click(object sender, EventArgs e)
{  
    count = (count - 1) >= 0 ? count - 1 ? files.Length - 1;

    DisplayNextFile(count);
}

Replace the for loop with:
picBoxMainPreview.Image = Image.FromFile(c);

