本文介绍了从控件中取消事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

this.comboBox1.SelectedIndexChanged + = new System.EventHandler(this.comboBox1_SelectedIndexChanged);



以上语句将为事件的控件comboBox1连接事件SelectedIndexChanged如果我们再次写同一个语句,那么方法comboBox1_SelectedIndexChanged将被调用两次,就像我们被触发的SelectedIndexChanged一样。



现在我想unhood / unbind与comboBox1相关的所有事件。



this.comboBox1。 SelectedIndexChanged - = new System.EventHandler(this.comboBox1_SelectedIndexChanged);



使用上面的语句将统一事件一次,所以我想知道我们是否有任何可以解开该控件中的所有事件。



提到的波纹管只是从控件中删除了点击事件。



对象OBJ = f1.GetValue(comboBox1);

的PropertyInfo PI = comboBox1.GetType()的getProperty( 事件,BindingFlags.NonPublic | BindingFlags.Instance);

EventHandlerList list =(EventHandlerList)pi.GetValue(comboBox1,null);

list.RemoveHandler(obj,list [obj]);

解决方案



this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);

The above statement will hookup the event for the control comboBox1 for the event SelectedIndexChanged and if we write the same statement again, then the method "comboBox1_SelectedIndexChanged" will be called twice, as an when the SelectedIndexChanged us being fired.

Now I wanted to unhood/ unbind all the events associated to comboBox1.

this.comboBox1.SelectedIndexChanged -= new System.EventHandler(this.comboBox1_SelectedIndexChanged);

Using the above statement will unhood the event only once, so I wanted to know if any how we can unhook all the events from that control.

The bellow mentioned is removing only the click event from the control.

object obj = f1.GetValue(comboBox1);
PropertyInfo pi = comboBox1.GetType().GetProperty("Events", BindingFlags.NonPublic | BindingFlags.Instance);
EventHandlerList list = (EventHandlerList)pi.GetValue(comboBox1, null);
list.RemoveHandler(obj, list[obj]);

解决方案



这篇关于从控件中取消事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 19:06