我的程序有10張圖片,顯示來自我想要的文件夾的圖片。添加下一個按鈕和上一個按鈕允許用戶瀏覽接下來的十張圖片或最近的十張圖片。前十張圖片顯示成功,但下一個按鈕僅在該文件夾正好有二十張圖片時才起作用,如果圖片爲十五張,則會崩潰。 這裏是我的代碼:一個顯示十個圖片箱上的很多圖片
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]);
}
}
}
沒有線索爲前一個按鈕。
後的錯誤。 – Blorgbeard
似乎是對我的家庭作業。給定的代碼不會編譯,並且缺少一些關鍵細節('i2'設置或修改的位置?)。 –
爲什麼你不使用'ImageList'? –