Results 1 to 2 of 2

Thread: Retrieving the EventHandler for any Event by code.

  1. #1

    Thread Starter
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Retrieving the EventHandler for any Event by code.

    Here's a snippet that'll return the a delegate pointing to the eventhandler for a specific controls event.

    the eventname parameter must be the name of the event you want to receive the EventHandler for, preceded with "Event". So if you want to retrieve the EventHandler the Click event, you pass "EventClick" as the second parameter.
    VB.NET Code:
    1. Private Function GetEventHandler(ByVal ctrl As Control, ByVal eventname As String) As [Delegate]
    2.         Dim propInfo As System.Reflection.PropertyInfo = GetType(System.ComponentModel.Component).GetProperty("Events", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance Or Reflection.BindingFlags.Static)
    3.         Dim handlerList As System.ComponentModel.EventHandlerList = CType(propInfo.GetValue(ctrl, Nothing), System.ComponentModel.EventHandlerList)
    4.         Dim controlEventInfo As System.Reflection.FieldInfo = GetType(Control).GetField(eventname, Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Static)
    5.  
    6.         If controlEventInfo Is Nothing Then
    7.             Throw New ArgumentException("Specified event does not exist.")
    8.         End If
    9.  
    10.         Dim eventKey As Object = controlEventInfo.GetValue(ctrl)
    11.         Dim EventHandlerDelegate As [Delegate] = handlerList.Item(eventKey)
    12.         Return EventHandlerDelegate
    13.     End Function

    I might just add a little explanation of how this works too...
    All controls inherit from System.ComponentModel.Component. This Component class has a special member...a field called "events", of the type System.ComponentModel.EventHandlerList. We can not access this field as it is private, but we can access its protected property "Events" through reflection.
    So after line #2 has been executed we have a PropertyInfo object for the Components Events property.
    Line #3 retrieves the value from this property, which will be of the type mentioned above, EventHandlerList.

    To retrieve an EventHandler from the EventHandlerList, we must pass a key to it, this key is a special object found in the Control class. The Control class has one member like this for each Event; EventClick, EventDragDrop, EventMouseDown etc.
    Line #4 retrieves a FieldInfo object for one of these objects based on what eventname you passed to the function, if this returns Nothing, we know that the eventname argument refers to an event that doesnt exist.
    Line #10 retrieves the actual object in our control of choice that the newly retried FieldInfo object refers too.
    On line #11 we simply use this object as a key in the EventHandlerList's Item property and a Delegate is returned!
    Last edited by Atheist; Jun 4th, 2008 at 09:31 AM.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  2. #2
    New Member
    Join Date
    Sep 2012
    Posts
    2

    Re: Retrieving the EventHandler for any Event by code.

    how examples of its use in the management of the event handler

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width