如何使用输入操作更改 vb.net 中的按钮颜色?

How do i change the button color in vb.net with an enter action?

我添加了一个按钮 (name: btn_exit) 并执行以下操作:如果 光标离开按钮 它应该变成红色,就像一个信号。在我的应用程序中,它不像我想的那样工作。控制台说没有错误。那么我要调整什么来改变颜色呢?

我的代码:

Private Sub btn_exit_Leave(sender As Object, e As EventArgs) Handles btn_exit.Leave
    btn_exit.BackColor = Color.Red
End Sub

您不能使用 'Leave' 事件执行此操作,您必须使用 MouseEnter、MouseHover、MouseLeave 事件。

示例代码(刚刚测试):按钮是 "normal",直到您第一次将鼠标移到它上面,然后变成蓝色,当您离开鼠标时,然后变成红色背景。

Private Sub Button1_MouseEnter(sender As Object, e As EventArgs) Handles Button1.MouseEnter

    Button1.BackColor = Color.Blue

End Sub

Private Sub Button1_MouseLeave(sender As Object, e As EventArgs) Handles Button1.MouseLeave

    Button1.BackColor = Color.Red

End Sub 

试试这个:

Private Sub Button1_MouseEnter(sender As Object, e As EventArgs) Handles Button1.MouseEnter

  Button1.BackColor = Color.Blue

End Sub

Private Sub Button1_MouseLeave(sender As Object, e As EventArgs) Handles Button1.MouseLeave

  Button1.BackColor = Color.Red

End Sub