Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I'm making a program in vb.net! But with every button you have there's a event handler (ex. handles button.click)

Isn't there a way that i can collect all buttons so I can call this collection 'AllButtons' and that I only have to type this 'handles' one time.(ex handles AllButtons.click)

 

Thnx & grtz from Jos

  • Administrators
Posted

You could add the handlers in code as an alternative

 

Private Sub Buttons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
       MessageBox.Show("dsfsdf")
   End Sub

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       Dim c As Control
       For Each c In Me.Controls
           If TypeOf c Is Button Then
               AddHandler c.Click, AddressOf Buttons_Click
           End If
       Next
   End Sub

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

Sorry Me again ....

 

I don't know if there's any advantage by using the abov code , because every button has a different function so i don't know if this is 'programmatically' a good way do to this. I was testing if it was possible, but afterwards I thought 'Is this actually programmatically a right thing to do'

 

 

 

Grtzz Josso

  • Administrators
Posted

If every button needs to do something different then you are probably better going with one click event per button.

If multiple controls require the same (or simialr) event processing then code like the above snippet can be quite useful.

 

Really boils down to if it works here - use it here, if not - don't.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

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...