|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
have a variable reference to an eventI am using "AddHandler Event, AddressOf Method" to register events but how
do I go about making the Event parameter be a variable. My user interface shows a list of available events for a class, through remoting in a drop down. The user selects an event from a list at which point I want to dynamicaly bind that event to a known method. Looking for something like this: Dim e as Event = MyObject.Event 'user selects objct instance and event through the UI AddHandler e, AddressOf Method 'code I execute after user selection Obviously the above syntax does not work. Perry Nevermind, I figured it out. For others that may need this here a code
snippet that demonstrates how this can be done. Note that events that I permit for users to bind use the custom attribute "ScriptEventAttribute". With this aproach if you have a drop down in your UI that lists the events for the user to bind, you don't have to change any code to add new events that are bindable by the user. Just add the attribute to the events you want to expose. Reflections takes care of the rest. Imports System.ComponentModel Public Class Form1 Delegate Sub ReceivedEventHandler(ByVal obj As test) Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim obj As New test obj.Name = "Perry" Dim objEventList As EventDescriptorCollection = TypeDescriptor.GetEvents(obj.GetType, New Attribute() {New ScriptEventAttribute(True)}) For Each objEvent As EventDescriptor In objEventList If objEvent.Name = "Event1" Then '"Event1" can be given by user objEvent.AddEventHandler(obj, New ReceivedEventHandler(AddressOf ReceivedEvent)) End If Next obj.RaiseEvent1() End Sub Private Sub ReceivedEvent(ByVal obj As test) MsgBox(obj.Name) 'shows that indeed our object has been raised End Sub Class ScriptEventAttribute Inherits Attribute Private _blnIsBindable As Boolean 'serves for purpose of signature comparison Sub New(ByVal blnIsBindable As Boolean) _blnIsBindable = blnIsBindable End Sub End Class Class test Private _strName As String <ScriptEvent(True)> _ Event Event1 As ReceivedEventHandler <ScriptEvent(True)> _ Event Event2 As ReceivedEventHandler <ScriptEvent(False)> _ Event Event3 As ReceivedEventHandler 'not available for user binding Property Name() As String Get Return _strName End Get Set(ByVal value As String) _strName = value End Set End Property Sub RaiseEvent1() RaiseEvent Event1(Me) End Sub End Class End Class Perry Show quote "Perecli Manole" <Pere***@dslextreme.com> wrote in message news:%23BgqefjaHHA.808@TK2MSFTNGP04.phx.gbl... >I am using "AddHandler Event, AddressOf Method" to register events but how >do I go about making the Event parameter be a variable. My user interface >shows a list of available events for a class, through remoting in a drop >down. The user selects an event from a list at which point I want to >dynamicaly bind that event to a known method. > > Looking for something like this: > Dim e as Event = MyObject.Event 'user selects objct instance and event > through the UI > AddHandler e, AddressOf Method 'code I execute after user selection > > Obviously the above syntax does not work. > > Perry > |
|||||||||||||||||||||||