我的程序有十张图片,它们显示了我想要的文件夹中的图像。添加下一个按钮和上一个按钮允许用户浏览下十张图片或最后十张图片。前十张图片显示成功,但是下一个按钮只有在文件夹正好有二十张图片时才起作用,如果图片是十五张就会崩溃。这是我的代码:
PictureBox[] myPicBoxArray = new PictureBox[10];
string path = @"\\Documents\Pictures\Camera";
private void Form1_Load(object sender, EventArgs e)
{
myPicBoxArray[0] = pictureBox1;
myPicBoxArray[1] = pictureBox2;
myPicBoxArray[2] = pictureBox3;
myPicBoxArray[3] = pictureBox4;
myPicBoxArray[4] = pictureBox5;
myPicBoxArray[5] = pictureBox6;
myPicBoxArray[6] = pictureBox7;
myPicBoxArray[7] = pictureBox8;
myPicBoxArray[8] = pictureBox9;
myPicBoxArray[9] = pictureBox10;
}
//Show button to display first ten pictures
private void showButton_Click(object sender, EventArgs e)
{
string[] files = Directory.GetFiles(path);
int i = 0;
foreach (string ofile in files)
{
myPicBoxArray[i].SizeMode = PictureBoxSizeMode.StretchImage;
myPicBoxArray[i].Image = Image.FromFile(files[i]);
i++;
if (i >= 10)
break;
}
}
private void nextButton_Click(object sender, EventArgs e)//The problem is here,
{
DirectoryInfo fileDir = new DirectoryInfo(path);
while (i2 < 10)
{
myPicBoxArray[i2].SizeMode = PictureBoxSizeMode.StretchImage;
if (i2 + 10 < picArrFileNames.Length)
{
myPicBoxArray[i2].Image = Image.FromFile(picArrFileNames[i2 + 10]);
}
}
}
上一个按钮没有线索。