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. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 对XML和YAML文件实现I/O操作
- reactjs – 如何使用react-test-renderer / shallow与types
- Cocos2d-x --Action (动作)
- reactjs – React中是否仍然需要使用自动绑定和属性初始值设
- React中文教程 - Advanced Components(组件高级使用)
- Flex 日期格式化
- Flex文本框自动提示(AutoSuggest)、自动完成(AutoComplete)
- c – 没有删除操作符的shared_ptr内存泄漏
- cocos2d-js-----Uncaught TypeError: Cannot read property
- swift – 将隐式解包的变量传递给inout参数不会编译