Collecting all buttons in one variabele

Jos

Member
Joined
Jan 12, 2004
Messages
17
Im making a program in vb.net! But with every button you have theres a event handler (ex. handles button.click)
Isnt 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
 
You could add the handlers in code as an alternative

Code:
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
 
Sorry Me again ....

I dont know if theres any advantage by using the abov code , because every button has a different function so i dont 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
 
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 - dont.
 
Back
Top