Manipulate Multiple Windows Forms Controls

This code simply demonstrates how to ask the user for a colour, and then enumerate all the button controls within a panel setting their background colour.

'place a panel on your form called pnlObjects
'in that panel place some buttons
'add a button to you form and call it cmdChangeColour
Private Sub cmdChangeColour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdChangeColour.Click
            Dim cDlg As New ColorDialog
            Dim selColour As Color = Color.White
            'get the user colour
            With cDlg
                .AnyColor = True
                selColour = .Color
            End With
            'set all the controls in the panel to that colour
            For Each c As Control In pnlObjects.Controls
                If TypeOf c Is Button Then
                    Dim b As Button = CType(c, Button)
                    With b
                        .BackColor = selColour
                    End With
                End If
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
    End Sub
End Class

You might also like...




Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Most software today is very much like an Egyptian pyramid with millions of bricks piled on top of each other, with no structural integrity, but just done by brute force and thousands of slaves” - Alan Kay