locked
How to define attached some event handler to event or not? RRS feed

  • Question

  • Hello.
    I have a button, for example (or any other visual control). I need ability to check if some event (Button.Click, for example) has attached some event handler to it or not. See example:
    ...
    Button btn = new Button();
    btn.Name = "btnTest";
    ...
    if (btn.Click == null) btn.Click += SomeEventHandler;   //Here the question is
    ...
    But I can not write "btn.Click == null" directly because events supports just += and -= operations.
    How can I do this check?
    Thursday, February 21, 2008 5:21 AM

Answers

  • All the eventhandlers for a control are saved in the private field events. The events field is declared within the Component class (Button derives from Control which derives from Component). The events field is of type EventHandlerList.

     

    Since the field is private it is not directly accessible. The only way to get its value is by using reflection. With the following code, you can retrieve the events field (testButton is an instance of a Button):

     

    PropertyInfo eventsProperty = typeof(Control).GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic);

    EventHandlerList events = (EventHandlerList)eventsProperty.GetValue(testButton, null);

     

    You can compare the EventHandlerList to a dictionary. So to be able to get the handlers for the click event, you first need to know the key for the click event. You can get the key by using the following code:

     

    FieldInfo eventKeyInfo = typeof(Control).GetField("EventClick", BindingFlags.Static | BindingFlags.NonPublic);

    object eventKey = eventKeyInfo.GetValue(testButton);

     

    With this information, you can retrieve the handlers that are associated to the click event by using the following code:

    Delegate buttonClickEvent = events[eventKey];

    Delegate[] listOfAssignedHandlers = buttonClickEvent.GetInvocationList();

    foreach (Delegate handler in listOfAssignedHandlers)

    {

    MessageBox.Show(handler.Method.ToString());

    }

     

    Hope this helps!

     

    Geert

     

     

    Geert Verhoeven
    Consultant @ Ausy Belgium

    My Personal Blog

    Thursday, February 21, 2008 8:09 AM

All replies

  • All the eventhandlers for a control are saved in the private field events. The events field is declared within the Component class (Button derives from Control which derives from Component). The events field is of type EventHandlerList.

     

    Since the field is private it is not directly accessible. The only way to get its value is by using reflection. With the following code, you can retrieve the events field (testButton is an instance of a Button):

     

    PropertyInfo eventsProperty = typeof(Control).GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic);

    EventHandlerList events = (EventHandlerList)eventsProperty.GetValue(testButton, null);

     

    You can compare the EventHandlerList to a dictionary. So to be able to get the handlers for the click event, you first need to know the key for the click event. You can get the key by using the following code:

     

    FieldInfo eventKeyInfo = typeof(Control).GetField("EventClick", BindingFlags.Static | BindingFlags.NonPublic);

    object eventKey = eventKeyInfo.GetValue(testButton);

     

    With this information, you can retrieve the handlers that are associated to the click event by using the following code:

    Delegate buttonClickEvent = events[eventKey];

    Delegate[] listOfAssignedHandlers = buttonClickEvent.GetInvocationList();

    foreach (Delegate handler in listOfAssignedHandlers)

    {

    MessageBox.Show(handler.Method.ToString());

    }

     

    Hope this helps!

     

    Geert

     

     

    Geert Verhoeven
    Consultant @ Ausy Belgium

    My Personal Blog

    Thursday, February 21, 2008 8:09 AM
  • Thank you for your help. This is exactly what I need. Yau are good man Smile
    Thursday, February 21, 2008 10:31 AM
  •  

    Here is one more question on the same theme:

    How to do same if there is no property "Events", like in most of office controls?

    I try GetEvent() but found nothing useful.

    Tuesday, February 26, 2008 11:23 AM