tags:

views:

68

answers:

3

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];
    }
A: 
imageOne.ImageLocation = (currentImage - 3 < 0) ? null : pictures[currentImage - 3];
....
imageFour.ImageLocation = (currentImage + 2 >= pictures.Count) ? null : pictures[currentImage + 2];

etc.

BlueRaja - Danny Pflughoeft
+2  A: 

Well, one option would be to use a helper method to ensure that the value is always within bounds:

string GetPictureAt(int index)
{
    // Copes with values which are two large or too small,
    // but only as far as -pictures.Length
    if (index < 0)
    {
        index += pictures.Length;
    }
    return pictures[index % pictures.Length];
}

Then:

public void CycleImagesLeft()
{
    int currentImage = pictures.IndexOf(imageThree.ImageLocation);
    imageOne.ImageLocation = GetPictureAt(currentImage - 3);
    imageTwo.ImageLocation = GetPictureAt(currentImage - 2);
    // etc
}

and the same for CycleImagesRight(). I think this does what you want, but I didn't quite follow your penultimate sentence.

Note that you still need to account for the possibility of there being fewer than 5 pictures.

Jon Skeet
Jon, do you mean to use the .Count properties of my List<string> pictures?
Sergio Tapia
Jon, this is working 100% fine, but I don't really understand why the modulus operator works in this situation. I thought it's use was to find remainders after a division; how is this magic working?
Sergio Tapia
@Sergio Tapia: Modulus ensures that your index is always in the range between 0 and `List.Length`.
R0MANARMY
+1  A: 

Could always use a Circular List. At least that way all the index management stuff is abstracted away (into a testable and re-usable class).

R0MANARMY
+1 for showing us the already made wheel. While this question is fun to think through, it has been solved thousands of times before.
Hamish Grubijan