首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C# winform 单击按钮防止多次实例化()

2013-11-14 
C# winform 单击按钮防止多次实例化(在线等)一个单击按钮,希望list中值可以多次添加,但每次单击都会重新实

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<ImageListshow>();
定义成 全局变量

热点排行