Iteratively set properties of command buttons in a Form
Posted on 2006-06-15
I'm trying to iteratively set properties of command buttons on my form using the code shown. The code compiles, but I get a run time error on the inner For statement with a variable of type Button iterating through the form's controls. If I try using a variable of type Control, the code doesn't build, because the ForeColor property is not recognized for Controls.
How can I loop through the buttons on my form, setting this property?
Private Sub EmphasizeButton(ByVal CommandButton as Button)
Dim intASCValue as Integer
Dim btn as Button
For IntASCValue = 65 to 90
For Each btn in Me.Controls '***** Run time error here -- Specified Cats is not Valid
If btn.ID = "cmd" & Chr(intASCValue) or btn.ID = "cmdAll" Then
'*** (ForeColor property is not recognized if btn is declared as type Control)
btn.ForeColor = System.Drawing.Color.Black
CommandButton.ForeColor = System.Drawing.Color.Red