Hi guys,
What I wanna do now is to change the button's color when the button is clicked, and resume its original color when other button being clicked.
Noted that on my form I have many buttons, but I only want the above condition happens on 3 buttons of them. It's a bit like a "button group"
So it will be easy to figure out which button is the last one I clicked of them three. Say, that button's color will be the changed color and the other two will have their original color.
I tried to use the following code to achieve this:
But when I run the program I encounter an exception: InvalidCastException was unhandled
"Unable to cast object of type 'System.Windows.Forms.PictureBox' to type 'System.Windows.Forms.Button'." in the line
On my form, I not only have several buttons, but also have a few picture boxes. I tried to use "If TypeOf b Is System.Windows.Forms.Button Then" to modify it. But if I put it inside the for each loop, the exception is still there, and if I put it outside the for each loop, b is not declared in the statement.
Could anybody help me out? Thanks a lot!!
What I wanna do now is to change the button's color when the button is clicked, and resume its original color when other button being clicked.
Noted that on my form I have many buttons, but I only want the above condition happens on 3 buttons of them. It's a bit like a "button group"
So it will be easy to figure out which button is the last one I clicked of them three. Say, that button's color will be the changed color and the other two will have their original color.
I tried to use the following code to achieve this:
VB.NET:
Private Sub ColorButtons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
For Each b As Button In Me.Controls
If b.Tag Is "xx" Then b.BackColor = Color.Green
Next
CType(sender, Button).BackColor = Color.Red
End Sub
"Unable to cast object of type 'System.Windows.Forms.PictureBox' to type 'System.Windows.Forms.Button'." in the line
VB.NET:
For Each b As Button In Me.Controls
On my form, I not only have several buttons, but also have a few picture boxes. I tried to use "If TypeOf b Is System.Windows.Forms.Button Then" to modify it. But if I put it inside the for each loop, the exception is still there, and if I put it outside the for each loop, b is not declared in the statement.
Could anybody help me out? Thanks a lot!!