Event Handler

F

Fei

HI,

I have a problem about EventHandler. I have a form with one button on it.
I implement Button's click event. Is there a way I can determine whether I
implement a handler for this event at runtime ? I try to use

1. me.Events.Item(Button1) . This will return Nothing

2. me.Events.Item(Button1.Click). This will throw out an exception
saying 'Click' is not a member of 'Button'

Please help. Thanks !

Fei
 
M

Matt S

First create your ebvent handler method, but do do not attach it to any
event at this stage:

-- Code Starts --
Private Sub TestRuntimeEventHandler(ByVal sender As System.Object, ByVal e
As System.EventArgs)
' Insert your event handliong code here
End Sub
-- Code ends --

Make sure the method has these parameters otherwise it will not work,

And then at runtime, whereever you want to add the event handler, add the
following code:

-- Code Starts --
AddHandler Button1.Click, AddressOf TestRuntimeEventHandler
-- Code Ends --

The AddHandler takes the event you want to handle and the name of the method
to handle it prefixed by 'AddressOf ' as parameters.

If you want to remove the event handler at runtime you use RemoveHandler is
a similar way.

Hope this helps.
 
F

Fei

Matt.

What Shall I put in Events.Item ?
Events.Item(*What is sppposed to be here*) ?


Thanks !
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top