加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 将对象的数组数传递给事件处理程序

发布时间:2020-12-15 22:38:09 所属栏目:百科 来源:网络整理
导读:我有一个PictureBoxes数组. for(int i = 0; i10; i++){ this.elevatorDoors[i] = new System.Windows.Forms.PictureBox();} 我想传递给一个对象的click事件,即特定对象具有的数组编号. this.elevatorDoors[i].Click += new System.EventHandler((sender,e)=t
我有一个PictureBoxes数组.

for(int i = 0; i<10; i++)
{ 
   this.elevatorDoors[i] = new System.Windows.Forms.PictureBox();
}

我想传递给一个对象的click事件,即特定对象具有的数组编号.

this.elevatorDoors[i].Click +=
      new System.EventHandler((sender,e)=>this.elevatorDoor_Click(sender,e,i));

但是当事件激发时我得到的只是数字10.我想在我点击第4张PictureBox时得到例如数字4.

解决方法

var tempValue = i;
this.elevatorDoors[i].Click +=
      new System.EventHandler((sender,e)=> this.elevatorDoor_Click(sender,tempValue));

这将解决它.因此,您必须确保在调用事件时,您将获取循环迭代的当前值.离开循环后,该值将始终等于最后一个值.所以你引入了存储当前迭代的tempValue.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读