Foro de elhacker.net

Programación => .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
  1.    Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.        MessageBox.Show("Hola")
  3.    End Sub

Usando la instrucción Handles

Yo quiero hacerlo así:

Código
  1.  
  2.  
  3.    Public Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  4.        MessageBox.Show("Hola")
  5.    End Sub
  6.  
  7.        Dim MethodName As String = "Button1_Click"
  8.        Dim HandlerType As Type = GetType(EventHandler)
  9.        Dim MethodI As MethodInfo = Me.GetType.GetMethod(MethodName)
  10.  
  11.    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  12.        Dim MethodName As String = "Button1_Click"
  13.        Dim HandlerType As Type = GetType(EventHandler)
  14.        Dim MethodI As MethodInfo = Me.GetType.GetMethod(MethodName)
  15.        Dim DelegateEvent As [Delegate] = _
  16.        [Delegate].CreateDelegate( _
  17.        HandlerType, _
  18.        MethodI, _
  19.        True)
  20.  
  21.        AddHandler Button1.Click, DelegateEvent
  22.  
  23. 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
  1.    Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2.        Dim MethodName As String = "Button4_Click"
  3.        Dim HandlerType As Type = GetType(EventHandler)
  4.        Dim MethodI As MethodInfo = Me.GetType.GetMethod(MethodName)
  5.        Dim DelegateEvent As [Delegate] = _
  6.        [Delegate].CreateDelegate( _
  7.        HandlerType, _
  8.        Me, _
  9.        MethodName)
  10.  
  11.        AddHandler Button4.Click, DelegateEvent
  12.  
  13.    End Sub