0

Hello I am making an imageviewer in c#. The thing is that works correctly, I can see on click the next image.

But for values = 5 and =-1 gives me an exception.

How to fix this my code is this?

I get this message

InvalidArgument=Value of '-1' is not valid for 'index'.

Code:

int I;
if (pictureBox2.Visible == true)
{
     I = I - 1;
     pictureBox2.Image = imageList2.Images[I];
}

This image list has 4 pictures with + goes up to the fourth then with the code above goes down to the first one.

When I press the button one more time it gives me the error message that I am describing how can I fix this?

4

2 回答 2

0

如果你还不想效仿 Alessandro 的例子,你可以随时尝试老式的 if/else if

if(I<0)
  I=4;
else if(I>4)
  I=0;

您只需在 I 值更改后插入该代码。

于 2013-09-27T21:50:56.277 回答
0

这使 I 在范围内:

I = I - 1;
I = I < 0 ? 0 : I >= imageList2.Images.Length ? imageList2.Images.Length - 1 : I;
于 2013-09-27T21:42:48.767 回答