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.
Private Function GetEventHandler(ByVal ctrl As Control, ByVal eventname As String) As [Delegate]
Dim propInfo As System.Reflection.PropertyInfo = GetType(System.ComponentModel.Component).GetProperty("Events", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance Or Reflection.BindingFlags.Static)
Dim handlerList As System.ComponentModel.EventHandlerList = CType(propInfo.GetValue(ctrl, Nothing), System.ComponentModel.EventHandlerList)
Dim controlEventInfo As System.Reflection.FieldInfo = GetType(Control).GetField(eventname, Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Static)
If controlEventInfo Is Nothing Then
Throw New ArgumentException("Specified event does not exist.")
Dim eventKey As Object = controlEventInfo.GetValue(ctrl)
Dim EventHandlerDelegate As [Delegate] = handlerList.Item(eventKey)
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!