private void Form1_Load(object sender, EventArgs e)
        {
            string path = Application.StartupPath.Substring(0,Application.StartupPath.Substring(0,Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
            path += @"\01.jpg";
            string path2 = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
            path2 += @"\02.jpg";
            Image ming = Image.FromFile(path,true);
            imageList1.Images.Add(ming);
            Image ming2 = Image.FromFile(path2, true);
            imageList1.Images.Add(ming2);
            imageList1.ImageSize = new Size(200,165);
            pictureBox1.Width = 200;
            pictureBox1.Height = 165;
                    }        private void button1_Click(object sender, EventArgs e)
        {
            int a = 0;
            if (a == (imageList1.Images.Count - 1))
            {
                a = 0;
            }
            else if(a<imageList1.Images.Count)
            {
                a = a + 1;
            }            this.pictureBox1.Image=this.imageList1.Images[a];
        }        private void button2_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = imageList1.Images[1];
        }
    }这是代码那里出错了啊,请大家帮忙

解决方案 »

  1.   

    ImageList没有2张图,所以不能用1,如果有1张图,用imageList1.Images[0];
      

  2.   

    就算我使用imageList1.Images[0];也是这样的提示错误啊,
      

  3.   

    看这代码是看不出问题,你在 private void Form1_Load(object sender, EventArgs e) 的最后,private void button1_Click(object sender, EventArgs e) 最前,private void button2_Click(object sender, EventArgs e)
    最前,这三处加上 Console.WriteLine(imageList1.Images.Count); 出错时看看控制台输出了什么。
      

  4.   

    Application.StartupPath.Substring(0,Application.StartupPath.Substring(0,Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
    这句话是干嘛啊,好像是不是使用错误了
      

  5.   


    我曾经也使用过ImageList,用下标取的时候,也是取不出来,也不知道什么原因;不过有解决办法:
    Image ming2 = Image.FromFile(path2, true);
      imageList1.Images.Add(ming2);
    你添加的时候这样子添加:
     imageList1.Images.Add("ming2",ming2);
    给它添加一个key进去
    取的时候通过key来取
    例如
    imageList1["ming2"]这样子就可以取出来了。
    很奇怪,我怀疑是.net的bug
      

  6.   

    怎么添加key啊,我新人啊,不懂,望大侠指点
      

  7.   

    imageList1.Images.Add("1", ming);
    imageList1.Images.Add("2", ming2);
      

  8.   


    不是写了个例子嘛,添加的时候,如楼上:imageList1.Images.Add("1", ming);
    取的时候,imageList1["1"]这样取