开发者

make control for button

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 ct开发者_运维知识库rl = button Then
      ctrl.backcolor = color.red
   End If     
Next

but still error. please help me


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜