Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

  • *Experts*
Posted

You can have something like this:

Dim btn As Control
For Each btn In Me.Controls
   If TypeOf btn Is Button Then
        AddHandler btn.Click, Addressof handlingsub
   end if
Next

  • Leaders
Posted

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 .

   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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...