动态更改vb.net中的按钮背景颜色

时间:2015-04-16 03:41:58

标签: vb.net button

我的.Net应用程序中有3个按钮,我想知道如何动态执行以下代码。

公共类frmTC

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Button1.BackColor = Color.Red
    Button2.BackColor = Color.Yellow
    Button3.BackColor = Color.Yellow
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Button1.BackColor = Color.Yellow
    Button2.BackColor = Color.Red
    Button3.BackColor = Color.Yellow
End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Button1.BackColor = Color.Yellow
    Button2.BackColor = Color.Yellow
    Button3.BackColor = Color.Red
End Sub

End Class

3 个答案:

答案 0 :(得分:2)

Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button3.Click
   Dim btn As Button = CType(sender, Button)
   SetColor(btn)
End Sub

Private Sub SetColor(Button button)
    Button1.BackColor = Color.Yellow
    Button2.BackColor = Color.Yellow
    Button3.BackColor = Color.Yellow
    button.BackColor =Color.Red
End Sub

答案 1 :(得分:0)

如果要调用“上方”按钮,可以使用PerformClick()方法调用“事件”。

有关详细信息,请访问Here

答案 2 :(得分:0)

您可以在一个处理程序中处理所有事件;

Private Sub Buttons_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click 

select Case sender.ToString

  Case "Button1"
    Button1.BackColor = Color.Red
    Button2.BackColor = Color.Yellow
    Button3.BackColor = Color.Yellow

  Case "Button2"
    Button1.BackColor = Color.Yellow
    Button2.BackColor = Color.Red
    Button3.BackColor = Color.Yellow

  Case "Button3"
    Button1.BackColor = Color.Yellow
    Button2.BackColor = Color.Yellow
    Button3.BackColor = Color.Red

End Sub

抱歉,我没有测试它的平台,但我想它可能会指导你朝着正确的方向发展。