2016-01-30 51 views
0

我會做一個程序,你可以選擇你的照片從電腦到圖像列表和列表視圖,然後如果你點擊任何照片,你可以在圖片框中查看它。開始時,我打開列表視圖中的多張照片的問題首先點擊第一張照片是沒有問題的,但是當你想裏面顯示的二次圖象到圖片框,然後我得到這個錯誤:ArgumentOutOfRangeException發生

ArgumentOutOfRangeException發生

異常拋出: 'System.ArgumentOutOfRangeException' 在 System.Windows.Forms.dll中

其他信息: '0'= InvalidArgument值是無效的 '索引'。

你能幫幫我嗎?

public partial class TimeLapseForm : Form 
    { 
     public TimeLapseForm() 
     { 
      InitializeComponent(); 
     } 

     int resimSayac = 0; 
     List<string> resimKonumu = new List<string>(); 

     private void TimeLapseForm_Load(object sender, EventArgs e) 
     { 
      openFileDialog1.Multiselect = true; 
      openFileDialog1.Filter = "Jpeg Files|*.jpg;|Png Files|*.png;|Bitmap Files|*.bmp"; 
     } 

     private void browseBttn_Click(object sender, EventArgs e) 
     { 
      if (openFileDialog1.ShowDialog() == DialogResult.OK) 
      { 
       foreach (string DosyaYolu in openFileDialog1.FileNames) 
       { 
        imagelist1.Images.Add(DosyaYolu, Image.FromFile(DosyaYolu)); 
        resimKonumu.Add(DosyaYolu); 
        ListViewItem kucukresim = new ListViewItem(); 
        kucukresim.ImageIndex = resimSayac; 
        listView1.Items.Add(kucukresim); 
        resimSayac++; 
       } 
      } 
     } 

     private void fullScreenToolStripMenuItem_Click(object sender, EventArgs e) 
     { 
      FormBorderStyle = FormBorderStyle.None; 
      WindowState = FormWindowState.Maximized; 
      TopMost = true; 
     } 

     private void TimeLapseForm_KeyDown(object sender, KeyEventArgs e) 
     { 
      if (e.KeyCode == Keys.Escape) 
      { 
       FormBorderStyle = FormBorderStyle.Sizable; 
       WindowState = FormWindowState.Maximized; 
       TopMost = true; 
      } 
      else if (e.KeyCode == Keys.F11) 
      { 
       FormBorderStyle = FormBorderStyle.None; 
       WindowState = FormWindowState.Maximized; 
       TopMost = true; 
      } 
     } 

     private void exitToolStripMenuItem_Click(object sender, EventArgs e) 
     { 
      DialogResult dialog = MessageBox.Show("Are you sure to exit program?", "Exit", MessageBoxButtons.YesNo); 

      if (dialog == DialogResult.Yes) 
      { 
       Application.ExitThread(); 
      } 
      else if (dialog == DialogResult.No) 
      { 
       return; 
      } 
     } 

     private void listView1_SelectedIndexChanged_1(object sender, EventArgs e) 
     { 
      pictureBox1.Image = Image.FromFile(resimKonumu[listView1.SelectedIndices[0]]); 
     } 
    } 
} 
+0

尋求調試幫助的問題(「爲什麼代碼不工作?」)必須包含所需的行爲,特定的問題或錯誤以及在問題本身中重現問題所需的最短代碼。沒有明確問題陳述的問題對其他讀者無益。請參閱:如何創建最小,完整和可驗證示例。 – DIDoS

回答

3

您應該保護listView1_SelectedIndexChanged_1處理程序與未選定的索引。

private void listView1_SelectedIndexChanged_1(object sender, EventArgs e) 
{ 
    if (listView1.SelectedIndices.Count == 0) 
    { 
     return; 
    } 

    pictureBox1.Image = Image.FromFile(resimKonumu[listView1.SelectedIndices[0]]); 
} 
+0

你保存了我的項目:)謝謝你的隊友 –

相關問題