none
Copying same code to other buttons RRS feed

  • Question

  • I've got about 50 buttons in my form. All they do is change color when they are clicked. All the code for the buttons is the same except for the button numbers. Is there a way to apply the code to all of them at once or must I copy and paste it into every button?
    Tuesday, January 30, 2018 1:22 AM

Answers

  • Could you explain that in simpler terms or give me an example?

    You can add the button click events to the declaration of the event handler method, as many as you want.

        Private Sub Button_Click(sender As Object, e As EventArgs) Handles _
            Button1.Click, _
            Button2.Click, _
            Button3.Click

    Or you can add the handler method to each click event separately (obviously, in a loop if your buttons are already in a collection - likely, if you have 50 of them!):

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            AddHandler Button1.Click, AddressOf Button_Click
            AddHandler Button2.Click, AddressOf Button_Click
            AddHandler Button3.Click, AddressOf Button_Click
        End Sub
    
        Private Sub Button_Click(sender As Object, e As EventArgs)
            Dim B As Button = CType(sender, Button)
            If B.BackColor = Color.Red Then
                B.BackColor = Color.Green
            Else
    B.BackColor = Color.Red End If End Sub
    In either case the handler method creates a variable of Type Button, casts the sender argument to that variable, and uses that to refer to the control that raised the event.
    • Edited by Acamar Tuesday, January 30, 2018 2:20 AM fmt
    • Marked as answer by Pilot160 Tuesday, January 30, 2018 3:11 AM
    Tuesday, January 30, 2018 2:19 AM
  • Hello,

    Try the following. It handles buttons on the form and on panels, group boxes too.

    Public Class Form1
        Public Function GetAll(ByVal control As Control, ByVal type As Type) As IEnumerable(Of Control)
            Dim controls = control.Controls.Cast(Of Control)()
            Return controls.SelectMany(Function(ctrl) GetAll(ctrl, type)).
                Concat(controls).Where(Function(c) c.GetType() Is type)
        End Function
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            GetAll(Me, GetType(Button)).Cast(Of Button).
                ToList.
                ForEach(Sub(button)
                            AddHandler button.Click,
                                Sub(s As System.Object, a As EventArgs)
                                    Dim b As Button = CType(s, Button)
                                    If b.BackColor = Color.Red Then
                                        b.BackColor = Nothing
                                    Else
                                        b.BackColor = Color.Red
                                    End If
                                End Sub
                        End Sub)
        End Sub
    End Class

    You can even exclude one or more buttons

    Public Class Form1
        Private buttonToExclude As String() = {"Button2", "Button5"}
        Public Function GetAll(ByVal control As Control, ByVal type As Type) As IEnumerable(Of Control)
            Dim controls = control.Controls.Cast(Of Control)()
            Return controls.SelectMany(Function(ctrl) GetAll(ctrl, type)).
                Concat(controls).Where(Function(c) c.GetType() Is type)
        End Function
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            GetAll(Me, GetType(Button)).Cast(Of Button).
                ToList.
                ForEach(Sub(button)
                            AddHandler button.Click,
                                Sub(s As System.Object, a As EventArgs)
                                    If Not buttonToExclude.Contains(button.Name) Then
                                        Dim b As Button = CType(s, Button)
                                        If b.BackColor = Color.Red Then
                                            b.BackColor = Nothing
                                        Else
                                            b.BackColor = Color.Red
                                        End If
                                    End If
    
                                End Sub
                        End Sub)
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Tuesday, January 30, 2018 3:02 AM
    Moderator

All replies

  • Is there a way to apply the code to all of them at once or must I copy and paste it into every button?

    You can use the same event handler for all your buttons, either by listing them all in the Handles clause of the method declaration, or by adding the method to the event using the AddHandler statement.
    https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/addhandler-statement

    AddHandler is much easier if you already have your buttons in a collection, whichi is likely for that number on a form.

    The code in the event handler method gets the control that raised the event in the sender argument.  It seems that all you need to do is to cast the sender argument (it's an Object) to a variable that is a Button, and do the colour change.  If you need to distinguish the buttons for some reason then you could put an idetifier in the Tag property of each button, and consult that from your code.

    Tuesday, January 30, 2018 1:39 AM
  • Could you explain that in simpler terms or give me an example? I've only been using visual basic for a couple days now.
    Tuesday, January 30, 2018 1:52 AM
  • Could you explain that in simpler terms or give me an example?

    You can add the button click events to the declaration of the event handler method, as many as you want.

        Private Sub Button_Click(sender As Object, e As EventArgs) Handles _
            Button1.Click, _
            Button2.Click, _
            Button3.Click

    Or you can add the handler method to each click event separately (obviously, in a loop if your buttons are already in a collection - likely, if you have 50 of them!):

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            AddHandler Button1.Click, AddressOf Button_Click
            AddHandler Button2.Click, AddressOf Button_Click
            AddHandler Button3.Click, AddressOf Button_Click
        End Sub
    
        Private Sub Button_Click(sender As Object, e As EventArgs)
            Dim B As Button = CType(sender, Button)
            If B.BackColor = Color.Red Then
                B.BackColor = Color.Green
            Else
    B.BackColor = Color.Red End If End Sub
    In either case the handler method creates a variable of Type Button, casts the sender argument to that variable, and uses that to refer to the control that raised the event.
    • Edited by Acamar Tuesday, January 30, 2018 2:20 AM fmt
    • Marked as answer by Pilot160 Tuesday, January 30, 2018 3:11 AM
    Tuesday, January 30, 2018 2:19 AM
  • Thanks!
    Tuesday, January 30, 2018 2:31 AM
  • Thanks!

    Pilot,

    Please mark the post(s) that answered your question using the Mark as Answer link below the post.

    :)


    Tuesday, January 30, 2018 2:50 AM
  • Hello,

    Try the following. It handles buttons on the form and on panels, group boxes too.

    Public Class Form1
        Public Function GetAll(ByVal control As Control, ByVal type As Type) As IEnumerable(Of Control)
            Dim controls = control.Controls.Cast(Of Control)()
            Return controls.SelectMany(Function(ctrl) GetAll(ctrl, type)).
                Concat(controls).Where(Function(c) c.GetType() Is type)
        End Function
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            GetAll(Me, GetType(Button)).Cast(Of Button).
                ToList.
                ForEach(Sub(button)
                            AddHandler button.Click,
                                Sub(s As System.Object, a As EventArgs)
                                    Dim b As Button = CType(s, Button)
                                    If b.BackColor = Color.Red Then
                                        b.BackColor = Nothing
                                    Else
                                        b.BackColor = Color.Red
                                    End If
                                End Sub
                        End Sub)
        End Sub
    End Class

    You can even exclude one or more buttons

    Public Class Form1
        Private buttonToExclude As String() = {"Button2", "Button5"}
        Public Function GetAll(ByVal control As Control, ByVal type As Type) As IEnumerable(Of Control)
            Dim controls = control.Controls.Cast(Of Control)()
            Return controls.SelectMany(Function(ctrl) GetAll(ctrl, type)).
                Concat(controls).Where(Function(c) c.GetType() Is type)
        End Function
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            GetAll(Me, GetType(Button)).Cast(Of Button).
                ToList.
                ForEach(Sub(button)
                            AddHandler button.Click,
                                Sub(s As System.Object, a As EventArgs)
                                    If Not buttonToExclude.Contains(button.Name) Then
                                        Dim b As Button = CType(s, Button)
                                        If b.BackColor = Color.Red Then
                                            b.BackColor = Nothing
                                        Else
                                            b.BackColor = Color.Red
                                        End If
                                    End If
    
                                End Sub
                        End Sub)
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Tuesday, January 30, 2018 3:02 AM
    Moderator