Hello!

I am new to programming and c# and im trying to create a application that will guide you through a school with pictures. So you would be able to press forward, backward, left and right and it will lead you to a new picture. What I dont understand is how to change the picture the second time i press the forward button for example. Now it just changes once.

The code i have is :

Image[] images = new Image[40];

private void btnFram_Click(object sender, EventArgs e)
{
images[0] = Image.FromFile(@"C:\Users\Administrator\Desktop\PICS\b.JPG");
images[1] = Image.FromFile(@"C:\Users\Administrator\Desktop\PICS\c.JPG");
images[2] = Image.FromFile(@"C:\Users\Administrator\Desktop\PICS\d.JPG");
images[3] = Image.FromFile(@"C:\Users\Administrator\Desktop\PICS\e.JPG");

picBild.Image = images[0];

}

Would be nice to know how to display picture 1 after 0 and so forth. I have tried a for loop but I didnt get it to work.

Grateful for help :)

Edited 5 Years Ago by __avd: Added [code] tags. Do wrap your programming code blocks within [code] ... [/code] tags

You need a global pointer (array index value) to images

Image[] images = new Image[40];
private int imageIndex; // Global index that points the current image (index of images array)

private void initImages()
{
    // Call initImages() before user can press any button
    // Load images
    images[0] = Image.FromFile(@"C:\Users\Administrator\Desktop\PICS\b.JPG");
    images[1] = Image.FromFile(@"C:\Users\Administrator\Desktop\PICS\c.JPG");
    // etc. until you have images[40] loaded

    // Show the initial image (assuming index 0)
    imageIndex = 0;
    picBild.Image = images[imageIndex];
}

private void buttonForward(object sender, EventArgs e)
{
    // Show the next image if there is one
    // Check the bounds
    if (imageIndex < 40)
    {
        // Next image
        imageIndex++;
        picBild.Image = images[imageIndex];
    }
}

private void buttonBack(object sender, EventArgs e)
{
    // Show the previous image if there is one
    // Check the bounds
    if (imageIndex > 0)
    {
        // Previous image
        imageIndex--;
        picBild.Image = images[imageIndex];
    }
}

HTH

Comments
Exactly!
Well done!
Glad to see you!

Just one question Teme64.
Would it not be better "to scroll around the pictures" with the Forward or Back buttons (setting the index when appropriate), or would that be just a matter of "taste"?

IMHO its a matter of "taste" how the application should behave.

Here's the "wrapping around" version of the buttons

private void buttonForward(object sender, EventArgs e)
{
    // Show the next image or wrap around to first image
    // Check the bounds
    if (imageIndex < 40)
    {
        // Next image
        imageIndex++;
        picBild.Image = images[imageIndex];
    }
    else
    {
        // Upper limit reached, wrap back to first image
        imageIndex = 0;
        picBild.Image = images[imageIndex];
    }
}

private void buttonBack(object sender, EventArgs e)
{
    // Show the previous image if there is one or wrap around to lats image
    // Check the bounds
    if (imageIndex > 0)
    {
        // Previous image
        imageIndex--;
        picBild.Image = images[imageIndex];
    }
    else
    {
        // Lower limit reached, wrap back to last image
        imageIndex = 40;
        picBild.Image = images[imageIndex];
    }
}
This question has already been answered. Start a new discussion instead.