日期:2014-05-17  浏览次数:20953 次

C# winform 单击按钮防止多次实例化(在线等)
一个单击按钮,希望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;

------解决方案--------------------
把你的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;
    }
}

------解决方案--------------------
List<ImageListshow> mlist=new List<Im