One Sub Handling Multiple Events

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pagates

    #1

    One Sub Handling Multiple Events

    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.CheckC hanged events.

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

    Thanks,
    PAGates
  • Herfried K. Wagner [MVP]

    #2
    Re: One Sub Handling Multiple Events

    "pagates" <pagates@discus sions.microsoft .com> schrieb:[color=blue]
    > 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.CheckC hanged events.
    >
    > Is this possible in VB.Net, or do all controls need to have a separate
    > event
    > that calls this single sub?[/color]

    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.

    --
    M S Herfried K. Wagner
    M V P <URL:http://dotnet.mvps.org/>
    V B <URL:http://classicvb.org/petition/>

    Comment

    Working...