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
        Try
            Dim cDlg As New ColorDialog
            Dim selColour As Color = Color.White
            'get the user colour
            With cDlg
                .ShowDialog()
                .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
            Next
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
    End Sub
End Class

You might also like...

Comments

 Richard

Contribute

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.

“Programs must be written for people to read, and only incidentally for machines to execute.”