GENERACION EVENTOS

20/07/2004 - 18:55 por CLAUDIA | Informe spam
Como genero eventos dinamicos en tiempo de ejecución?

Preguntas similare

Leer las respuestas

#6 Claudia
22/07/2004 - 19:40 | Informe spam
Muchísimas gracias :)
Utiliza AddHandler para asociar un metodo con el evento


del control. Por
ejemplo:

Private Sub ClickBoton( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs)

MessageBox.Show("Click")

End Sub

Private Sub CrearControl()

Dim boton As New Button

With boton
.Text = "Prueba"
.Name = "boton"
.Location = New Point(20, 20)
End With

AddHandler boton.Click, AddressOf ClickBoton

Me.Controls.Add(boton)

End Sub

Si el codigo del evento tambien lo tienes en la base de


datos puedes
compilar el codigo en tiempo de ejecucion para generar un


delegado y luego
usar AddHandler para asociarlo con el evento del control.


Por ejemplo:

Private Sub CrearControl()

Dim codigo As String = _
"Shared Sub ClickBoton(" & _
"ByVal sender As System.Object, " & _
"ByVal e As System.EventArgs) " & vbCrLf & _
"MessageBox.Show(""Click"")" & vbCrLf & _
"End Sub"
Dim boton As New Button

With boton
.Text = "Prueba"
.Name = "boton"
.Location = New Point(20, 20)
End With

AddHandler boton.Click, _
CType(CrearEventHandler(codigo, "ClickBoton",
GetType(EventHandler)), EventHandler)

Me.Controls.Add(boton)

End Sub

Private Function CrearEventHandler( _
ByVal fuentes As String, _
ByVal metodo As String, _
ByVal tipoDelegado As Type) As [Delegate]

Dim provider As New


Microsoft.VisualBasic.VBCodeProvider
Dim compiler As System.CodeDom.compiler.ICodeCompiler
provider.CreateCompiler
Dim options As New


System.CodeDom.Compiler.CompilerParameters
Dim result As System.CodeDom.Compiler.CompilerResults

options.GenerateInMemory = True
options.ReferencedAssemblies.Add


("System.Windows.Forms.dll")

fuentes = "Imports System" & vbCrLf & _
"Imports System.Windows.Forms" & vbCrLf & _
"Namespace Eventos" & vbCrLf & _
"Class Eventos" & vbCrLf & _
fuentes & vbCrLf & _
"End Class" & vbCrLf & _
"End Namespace"

result = compiler.CompileAssemblyFromSource(options,


fuentes)

If result.Errors.Count = 0 Then

Dim asm As System.Reflection.Assembly
Dim t As Type

asm = result.CompiledAssembly
t = asm.GetType("Eventos.Eventos")

Return [Delegate].CreateDelegate(tipoDelegado,


t.GetMethod(metodo))

Else

Throw New Exception(result.Errors.Item


(0).ToString)

End If

End Function

Eduardo A. Morcillo [MS MVP VB]
http://www.mvps.org/emorcillo


.

email Siga el debate Respuesta Responder a este mensaje
Ads by Google
Help Hacer una pregunta AnteriorRespuesta Tengo una respuesta
Search Busqueda sugerida