Test Foro de elhacker.net SMF 2.1

Programación => Programación General => .NET (C#, VB.NET, ASP) => Mensaje iniciado por: Keyen Night en 4 Septiembre 2011, 20:23 PM

Título: AddHandler Con [Delegate].CreateDelegate [Solucionado]
Publicado por: Keyen Night en 4 Septiembre 2011, 20:23 PM
Quiero crear hacer un Handler con Delegate, me explico, normalmente uno hace un Handler así:

Código (vb.net) [Seleccionar]
   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 Handles

Yo quiero hacerlo así:

Código (vb.net) [Seleccionar]
       

   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

Código (vb.net) [Seleccionar]
    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