One Sub Handling Multiple Events

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello All,

In C#, one function can handle events from multiple, as long as it has the
same delagate signature. For example, there might be a single event that
handles all the Checkbox.CheckChanged events.

Is this possible in VB.Net, or do all controls need to have a separate event
that calls this single sub?

Thanks,
PAGates
 
pagates said:
In C#, one function can handle events from multiple, as long as it has the
same delagate signature. For example, there might be a single event that
handles all the Checkbox.CheckChanged events.

Is this possible in VB.Net, or do all controls need to have a separate
event
that calls this single sub?

It's possible in VB.NET. There are two different ways. If declaration of
the variable holding the reference to the control contains the 'WithEvents'
keyword, simply add the control to the 'Handles' list of the event handler:

\\\
Private Sub Buttons_Click(...) Handles Button1.Click, Button2.Click
...
End Sub
///

Alternatively you can dynamically add the handler at runtime using the
'AddHandler' statement.
 

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

Back
Top