Handles

Phylum

Centurion
Joined
Jun 20, 2003
Messages
105
Location
Canada
Is it possible to have a handles clause that handles every control? For instance:

Private SUB Btn1_Click(BLAH) Handles BUTTON1

(Syntax may be incorrect)

What I want is an event handler that handles every button on the form

Private SUB Bnt1_Click(BLAH) Handles all buttons

WITHOUT Listing every control in the handles clause.

Possible?
 
You can have something like this:
Visual Basic:
Dim btn As Control
For Each btn In Me.Controls
    If TypeOf btn Is Button Then
         AddHandler btn.Click, Addressof handlingsub
    end if
Next
 
as mutant said you can add a handler , by going through the buttons in your control collection , here's a quick example , if you add a few buttons , the handler will still know which button was clicked as you would see in the messagebox .
Visual Basic:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim btn As Control
        For Each btn In Controls
            If TypeOf btn Is Button Then
                AddHandler btn.Click, AddressOf ClickEvent
            End If
        Next
    End Sub

    Private Sub ClickEvent(ByVal sender As System.Object, ByVal e As System.EventArgs)
        MessageBox.Show("i'm a click from " & sender.name)
    End Sub
 
Back
Top