Pass an Event as parameter
- by dkson
I have a class which bundles events and registers controls to easily register/unregister them, it's basically used this way:
Private Sub MyFocusHandler(ByVal sender As Object, ...)
...
End Sub
...
Dim b1 = new MyTextBox()
Dim b2 = new MyTextBox()
.... 'Lot of Controls'
Dim cr = new ControlRegistration()
cr.RegisterControl(b1)
cr.RegisterControl(b2)
.... 'Register a lot of controls'
cr.RegisterEvent("Focus",New EventHandler(AddressOf MyFocusHandler))
cr.RegisterEvent("Validate",New EventHandler(AddressOf MyValidateHandler))
So I don't have to add the handlers manually for each control.
The ControlRegistration-Class will cycle through the list of registered controls and check if a control has a registered event and then attach the eventhandler, something like:
...
For Each control in contols
Dim ev_info As Reflection.EventInfo = _
control.GetType().GetEvent(.GetType().GetEvent(event_name))
If Not (ev_info Is Nothing) Then
ev_info.AddEventHandler ...
...
My problem is that i am identifying the event by a string
Public Sub RegisterEvent(ByVal eventName As String, ByVal handler As [Delegate])
...
...
cr.RegisterEvent("Focus",New EventHandler(AddressOf MyFocusHandler))
I don't want to depend on the name of the event, since it is possibly that the name changes, and then things will break up.
Is there a way I can do this like:
Public Sub RegisterEvent(ByVal theEvent As ???, ByVal handler As [Delegate])
...
...
cr.RegisterEvent(IMyControl.MyEvent,New EventHandler(AddressOf MyEventHandler))
I hope it is clear what I want to archive. Anybody any ideas? Thanks