Quiero crear hacer un Handler con Delegate, me explico, normalmente uno hace un Handler así:
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show("Hola")
End Sub
Usando la instrucción
HandlesYo quiero hacerlo así:
Public Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
MessageBox.Show("Hola")
End Sub
Dim MethodName As String = "Button1_Click"
Dim HandlerType As Type = GetType(EventHandler)
Dim MethodI As MethodInfo = Me.GetType.GetMethod(MethodName)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim MethodName As String = "Button1_Click"
Dim HandlerType As Type = GetType(EventHandler)
Dim MethodI As MethodInfo = Me.GetType.GetMethod(MethodName)
Dim DelegateEvent As [Delegate] = _
[Delegate].CreateDelegate( _
HandlerType, _
MethodI, _
True)
AddHandler Button1.Click, DelegateEvent
End Sub
Cuando creo el
DelegateEvent da el error
Error al enlazar con el método de destino.No sé si este bien, ¿Qué está mal hecho?
Lo he logrado con este código
Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim MethodName As String = "Button4_Click"
Dim HandlerType As Type = GetType(EventHandler)
Dim MethodI As MethodInfo = Me.GetType.GetMethod(MethodName)
Dim DelegateEvent As [Delegate] = _
[Delegate].CreateDelegate( _
HandlerType, _
Me, _
MethodName)
AddHandler Button4.Click, DelegateEvent
End Sub