elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.
 
Inicio Ayuda Buscar Ingresar Registrarse
28 Mayo 2012, 08:28  


Tema destacado: Sigue las noticias más importantes de elhacker.net en ttwitter!

+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (Moderador: [D4N93R])
| | | |-+  AddHandler Con [Delegate].CreateDelegate [Solucionado]
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: AddHandler Con [Delegate].CreateDelegate [Solucionado]  (Leído 482 veces)
Keyen Night


Desconectado Desconectado

Mensajes: 315


Nothing


Ver Perfil
AddHandler Con [Delegate].CreateDelegate [Solucionado]
« en: 4 Septiembre 2011, 20:23 »

Quiero crear hacer un Handler con Delegate, me explico, normalmente uno hace un Handler así:

Código
    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
 
 
   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
    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


« Última modificación: 4 Septiembre 2011, 20:40 por Keyen Night » En línea

La Fé Mueve Montañas...
                                    ...De Dinero

[Aporte] Factorización Relativamente Rápida
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Powered by SMF 1.1.16 | SMF © 2006-2008, Simple Machines