一个单击按钮,希望list中值可以多次添加,但每次单击都会重新实例化,导致智能访问mlist[0]
            List<ImageListshow> mlist=new List<ImageListshow>();
            ImageListshow show1 = new ImageListshow();
            //show1.path = Connection.ConnectionPicture();
            show1.path = Image.FromFile(Connection.ConnectionPicture());
            show1.save = true;
            mlist.Add(show1);
            //pictureBox_get1.Image= mlist[0].path;
            string pictureBox = "pictureBox_get";
            pictureBox = pictureBox + i;
            PictureBox pb1 = (PictureBox)this.Controls.Find(pictureBox, true)[0];
            pb1.Image = mlist[0].path;

解决方案 »

  1.   

    把你的List<ImageListshow> mlist作为成员变量写在你的类里面:
     class AClass

        //成员变量
        List<ImageListshow> mlist;
        //构造方法
        AClass()
        {
            List<ImageListshow> mlist=new List<ImageListshow>();
        }
        //其它成员函数
        public void yourMethod()
        {
            mlist=new List<ImageListshow>();
            ImageListshow show1 = new ImageListshow();
            //show1.path = Connection.ConnectionPicture();
            show1.path = Image.FromFile(Connection.ConnectionPicture());
            show1.save = true;
            mlist.Add(show1);
            //pictureBox_get1.Image= mlist[0].path;
            string pictureBox = "pictureBox_get";
            pictureBox = pictureBox + i;
            PictureBox pb1 = (PictureBox)this.Controls.Find(pictureBox, true)[0];
            pb1.Image = mlist[0].path;
        }
    }
      

  2.   

    List<ImageListshow> mlist=new List<ImageListshow>();
    定义成 全局变量