Handles keyword.

B

bokiteam

Hi All,

I didn't use a "Handles" keyword in VB6. I can't sure its funciton.

Does it mean that any object behind "handles" keyword will run the same
code?

ex:
Private Sub A(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles B, C, D, E

// B , C, D, E objects will get the same message ?


END SUB


// Thank you very much!

// BR
// /Boki.
 
P

Paul Robson

Hi All,

I didn't use a "Handles" keyword in VB6. I can't sure its funciton.

Does it mean that any object behind "handles" keyword will run the same
code?

ex:
Private Sub A(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles B, C, D, E

// B , C, D, E objects will get the same message ?


END SUB

B,C,D and E are Events of an object declared withevents

e.g.

- private withevents txtEdit as TextBox

Declares a reference to a TextBox called txtEdit that has events
associated with it (e.g. KeyPress). When you declare your event handler
(say)

Private Sub X(....) handles txtEdit.KeyPressed

you are saying that the object that this refers to (created by txtEdit =
new TextBox()) is acquiring a (possibly additional) handler for when it
raises its KeyPress event.

The same Event Handler can handle multiple events ; this might be used
(say) if you had multiple text fields that you wanted to capitalise on
entry, for example.

Also, an Event can have multiple handlers.
 
H

Herfried K. Wagner [MVP]

I didn't use a "Handles" keyword in VB6. I can't sure its funciton.

Does it mean that any object behind "handles" keyword will run the same
code?

ex:
Private Sub A(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles B, C, D, E

// B , C, D, E objects will get the same message ?


END SUB

Yes.

I suggest to place the caret on 'Handles' and press the F1 key.
 
P

Phill W.

Does it mean that any object behind "handles" keyword will run the same
code?

If anything, it's the other way around.
Your method can handle any of the events that you list in the Handles
clause - usually one event per method (but for many objects) as in :

Private Sub AnyButton_Click( _
ByVal sender As System.Object _
, ByVal e As System.EventArgs _
) Handles button1.Click, button2.Click, button3.Click

This routine will be called whenever the user clicks on any of
button1, button2 or button3.

HTH,
Phill W.
 

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