Reputation: 45
I have wanted to ask my problem. I make 3 buttons, button 1, 2 and 3. so when I click one button automatic button changes color. I'm using code like this
For Each ctrl As Control In frm.Controls
If ctrl = button Then
ctrl.backcolor = color.red
End If
Next
but still error. please help me
Upvotes: 1
Views: 787
Reputation: 2302
The right code would be:
For Each ctrl As Control In frm.Controls
If TypeOf ctrl Is Button Then
DirectCast(ctrl,Button).BackColor = Color.Red
End If
Next
Upvotes: 1
Reputation: 25445
This isn't the best way. Have a look at the below option.
Sub buttons_click(sender as Object, e as event) Handles button1.Click,
_ button2.Click,
_ button3.Click
sender.backcolor = color.red
End Sub
Sorry if the syntax is a bit off, it's a while since i've done vb.
Hope this helps.
Upvotes: 0
Reputation: 30830
Use following code :
For Each ctrl As Control In Controls
If TypeOf ctrl Is Button Then
ctrl.BackColor = Color.Red
End If
Next
What you are doing wrong is compare an instance with a type. What you need to do is compare Type
of an instance to another Type
.
Upvotes: 0