E
Etienne-Louis Nicolet
(VB 2008 SP1)
I try to dinamically add / remove event handlers from a control. Since I
have the event as EventInfo type I can not use the AddHandler /
RemoveHandler statement.
I built a form having a DataGridView control named dgvData. My test form is
based on the VB-Help topic "How to: Hook Up a Delegate Using Reflection"
(ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/dv_fxadvance/html/076ee62d-a964-449e-a447-c31b33518b81.htm.)
Unfortunately an exception is thrown when creating an instance of the
delegate, and to be honest I have no clue how to approach the problem.
Here's the code snippet in the form:
Sub HookUpEvent()
' Get the EventInfo object representing the event
Dim evCellClick As EventInfo = Me.dgvData.GetEvent("CellClick")
' Use the EventHandlerType property to get the type of delegate used to
handle the event
Dim tDelegate As Type = evCellClick.EventHandlerType
' Get a MethodInfo object representing the method that handles the
event
Dim miHandler As MethodInfo = Me.GetType.GetMethod("dgvData_CellClick",
_
BindingFlags.NonPublic Or BindingFlags.Instance)
' Create an instance of the delegate, using the CreateDelegate method
' * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * *
' This statement throws an ArgumentException "Error binding to target
method"
' * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * *
Dim d As [Delegate] = [Delegate].CreateDelegate(tDelegate, Me.dgvData,
miHandler)
' Get the add accessor method and invoke it to hook up the event
Dim miAddHandler As MethodInfo = evCellClick.GetAddMethod()
Dim addHandlerArgs() As Object = {d}
miAddHandler.Invoke(Me.dgvData, addHandlerArgs)
End Sub
' To ensure that the signature is the same, I double-clicked the event in
the property window
' of the form designer and subsequently commented out the "... Handles
dgvData.CellClick"
' to make it a regular method
Private Sub dgvData_CellClick(ByVal sender as Object, _
ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) ' Handles
dgvData.CellClick
' Do something to verify if the event was trapped
End Sub
Thank you for any help and suggestions,
Etienne-Louis Nicolet
I try to dinamically add / remove event handlers from a control. Since I
have the event as EventInfo type I can not use the AddHandler /
RemoveHandler statement.
I built a form having a DataGridView control named dgvData. My test form is
based on the VB-Help topic "How to: Hook Up a Delegate Using Reflection"
(ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/dv_fxadvance/html/076ee62d-a964-449e-a447-c31b33518b81.htm.)
Unfortunately an exception is thrown when creating an instance of the
delegate, and to be honest I have no clue how to approach the problem.
Here's the code snippet in the form:
Sub HookUpEvent()
' Get the EventInfo object representing the event
Dim evCellClick As EventInfo = Me.dgvData.GetEvent("CellClick")
' Use the EventHandlerType property to get the type of delegate used to
handle the event
Dim tDelegate As Type = evCellClick.EventHandlerType
' Get a MethodInfo object representing the method that handles the
event
Dim miHandler As MethodInfo = Me.GetType.GetMethod("dgvData_CellClick",
_
BindingFlags.NonPublic Or BindingFlags.Instance)
' Create an instance of the delegate, using the CreateDelegate method
' * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * *
' This statement throws an ArgumentException "Error binding to target
method"
' * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * *
Dim d As [Delegate] = [Delegate].CreateDelegate(tDelegate, Me.dgvData,
miHandler)
' Get the add accessor method and invoke it to hook up the event
Dim miAddHandler As MethodInfo = evCellClick.GetAddMethod()
Dim addHandlerArgs() As Object = {d}
miAddHandler.Invoke(Me.dgvData, addHandlerArgs)
End Sub
' To ensure that the signature is the same, I double-clicked the event in
the property window
' of the form designer and subsequently commented out the "... Handles
dgvData.CellClick"
' to make it a regular method
Private Sub dgvData_CellClick(ByVal sender as Object, _
ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) ' Handles
dgvData.CellClick
' Do something to verify if the event was trapped
End Sub
Thank you for any help and suggestions,
Etienne-Louis Nicolet