问题
How can i check whether ComboBox.SelectIndexchanged Event does not holding any method.
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;
}
}
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.
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???
回答1:
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 {
//....
}
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
}
回答2:
Well you can check if any handler is attached to the event in the class where it the event is declared. If you try to do a check here you will get something like this:
The event SelectedIndexChanged can only appear on the left hand side of += or -=
Your best option is to have a dictionary to store the control and the event added for it.
来源:https://stackoverflow.com/questions/20673995/how-to-check-combobox-selectedindexchange-event-is-null-or-not