如何检查ComboBox.selectedIndexChange事件是否为null [英] How to check ComboBox.selectedIndexChange event is null or not

查看:149
本文介绍了如何检查ComboBox.selectedIndexChange事件是否为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何检查ComboBox.SelectIndexchanged事件是否不包含任何方法。

How can i check whether ComboBox.SelectIndexchanged Event does not holding any method.

在这里,我有一些方法可以向ComboBox添加和从Robomo方法中获取服务

Here i am having methods to Add and Rovemo methods to and from ComboBox which can serve for any comboBox.

 public static void AddMethodToComoBox(EventHandler MetodName, ComboBox cbm)
   {
       if(cbm.SelectedIndexChanged==null)
       {
       cm.SelectedIndexChanged += MetodName;
       }

   }
   public static void RemoveMethodToComoBox(EventHandler MetodName, ComboBox cbm)
   {
       if (cbm.SelectedIndexChanged != null)
       {
           cbm.SelectedIndexChanged -= MetodName;
       }

   }

如果我想添加方法意味着简单地我将调用此add方法并传递CmoboBox对象,并且该方法需要类似于Romove进行添加。

If i want to add a method means simply i will call this add method and pass CmoboBox object and Method need to Add similarly to Romove.

但是这里的问题是,如果我两次单击comboBox则该方法会打两次。
,以避免我检查ComboBox的selectedIndexChanged事件是否已保存任何Mthod。如果是,则代码将不会再次添加相同的方法。为此,我使用了If条件。但显示错误。
我该如何实现??

But the problem here is if i click a comboBox twice then the method will call twice. so to avoid that i am checking whether the ComboBox's selectedIndexChanged event is already holding any Mthod. If it is then code will not add the same method again. For that i used the If Condition. but it showing error. How can i achieve this???

推荐答案

您的问题是您需要访问<$ c $ ComboBox 中的c> EventHandlerList ,此事件处理程序列表未公开显示,因此我们必须进行一些反思。获取 SelectedIndexChanged 事件处理程序的键被保存为 EVENT_SELECTEDINDINDEXCHANGED 的字段。 > ComboBox 类,该字段也是非公开的,因此一旦获得 EventHanlderList SelectedIndexChanged 事件键,我们可以检查传入 EventHandlerList 的索引器的键是否返回null,返回null表示存在不是事件 SelectedIndexChanged 的任何处理程序:

Your problem is you need to access to the EventHandlerList of the ComboBox, this event handler list is not exposed publicly, so we have to use a little reflection. The key to get the handler of the SelectedIndexChanged event is saved as a field called EVENT_SELECTEDINDEXCHANGED in the ComboBox class, this field is also non-public, so we also have to use reflection to get it, once got both EventHanlderList and the SelectedIndexChanged event key, we can check if that key passing in the indexer of EventHandlerList returns null or not, returning null means there is not any handler for the event SelectedIndexChanged:

//Get the field EVENT_SELECTEDINDEXCHANGED
var eventSelectedIndexChangedKey = typeof(ComboBox).GetField("EVENT_SELECTEDINDEXCHANGED", 
                                   System.Reflection.BindingFlags.NonPublic |
                                   System.Reflection.BindingFlags.Static)
                                  .GetValue(comboBox1);
//Get the event handler list of the comboBox1
var eventList = typeof(ComboBox).GetProperty("Events",
                                 System.Reflection.BindingFlags.NonPublic | 
                                 System.Reflection.BindingFlags.Instance)
                                .GetValue(comboBox1, null) as EventHandlerList;
//check if there is not any handler for SelectedIndexChanged
if(eventList[eventSelectedIndexChangedKey] == null){
    //....
} else {
    //....
}

但是,我觉得您的问题仅仅是为避免为事件 SelectedIndexChanged 添加重复的(或两次)处理程序,因此您始终可以在分配之前先尝试注销该处理程序,这样永远不会抛出异常:

However, I feel that your problem is just to avoid adding duplicated (or twice) a handler for the event SelectedIndexChanged, so you can always try unregistering the handler first before assigning, it won't never throw exception:

public static void AddMethodToComoBox(EventHandler MetodName, ComboBox cbm)
{
   cm.SelectedIndexChanged -= MethodName;
   cm.SelectedIndexChanged += MethodName;
}
public static void RemoveMethodToComoBox(EventHandler MetodName, ComboBox cbm)
{
   cbm.SelectedIndexChanged -= MetodName;//won't never throw exception
}

这篇关于如何检查ComboBox.selectedIndexChange事件是否为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆