永发信息网

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

答案:3  悬赏:60  手机版
解决时间 2021-02-13 05:17
  • 提问者网友:一抹荒凉废墟
  • 2021-02-12 19:04
一个单击按钮,希望list中值可以多次添加,但每次单击都会重新实例化,导致只能访问mlist[0],而之后每次单击记录都在mlist【0】中,我希望实现第二次单击保存到mlist【1】中去
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;
最佳答案
  • 五星知识达人网友:狂恋
  • 2021-02-12 20:08
把这一行
List<ImageListshow> mlist=new List<ImageListshow>();


不要放在按钮单击事件里,挪到外面去就行了
也就是形如
List<ImageListshow> mlist=new List<ImageListshow>();
private void Button1_click(object sender,EventArgs e)  //这是你的单击
{  
  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;//我不e5a48de588b67a686964616f31333332643237太清楚你的用意,也行这里应该是pb.Image=mlist[mlist.Count-1].path;
}
全部回答
  • 1楼网友:蕴藏春秋
  • 2021-02-12 21:24
list and imagelist 可以是个整体,list item 中应该有image 这个属性事事整体弄
  • 2楼网友:一秋
  • 2021-02-12 20:58
belowdgcc.style.backcolor = color.gray;
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯