发生了ArgumentOutOfRangeException

时间:2016-01-30 12:58:41

标签: c#

我制作了一个程序,您可以从计算机到图像列表和列表视图中选择您的照片,然后如果您点击任何照片,您可以在图片框中查看它。当我在listview中打开多张照片时首先点击第一张照片没有问题,但是当你想在picturebox中显示二级图片时,我收到此错误:

  

发生了ArgumentOutOfRangeException

     

抛出异常:'System.ArgumentOutOfRangeException'中   System.Windows.Forms.dll中

     

其他信息:InvalidArgument =值为'0'无效   '索引'。

你能帮帮我们吗?

    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]]);
        }
    }
}

1 个答案:

答案 0 :(得分: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]]);
}