Index out of bounds exception while iterating through a List<string>.
Posted
by Sergio Tapia
on Stack Overflow
See other posts from Stack Overflow
or by Sergio Tapia
Published on 2010-06-16T02:52:23Z
Indexed on
2010/06/16
3:02 UTC
Read the original article
Hit count: 335
I have a List of the location of images on a folder.
I have five pictureBoxes that emulate a coverflow type area for users to skim through the images of a given folder.
I know that the error is fired because the first image in the collection is set to the first picturebox, then if I click on cycleLeft(), there is a negative number.
How can I account for this? For example, if the first image in the List is already set to the farthest left and someone clicks flip left, have that first image put on the last position of the list.
Any guidance?
private void leftArrow_Click(object sender, EventArgs e)
{
cycleImagesLeft();
}
private void rightArrow_Click(object sender, EventArgs e)
{
cycleImagesRight();
}
public void cycleImagesLeft()
{
//imageThree is the center image, that's why I use it as a frame of reference.
int currentImage = pictures.IndexOf(imageThree.ImageLocation);
imageOne.ImageLocation = pictures[currentImage - 3];
imageTwo.ImageLocation = pictures[currentImage - 2];
imageThree.ImageLocation = pictures[currentImage - 1];
imageFour.ImageLocation = pictures[currentImage];
imageFive.ImageLocation = pictures[currentImage + 1];
}
public void cycleImagesRight()
{
int currentImage = pictures.IndexOf(imageThree.ImageLocation);
imageOne.ImageLocation = pictures[currentImage - 1];
imageTwo.ImageLocation = pictures[currentImage];
imageThree.ImageLocation = pictures[currentImage + 1];
imageFour.ImageLocation = pictures[currentImage + 2];
imageFive.ImageLocation = pictures[currentImage + 3];
}
© Stack Overflow or respective owner