Apfel Apfel - 7 months ago 50
Vb.net Question

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

I added a Button (name:

) with the action: If the cursor leaves the button it should turn red, just as a signal. In my application that doesn't work like i thought. The Console says there no errors. So what do I to adjust to change the color ?

My Code:

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


You can not do this with the 'Leave' event, you have to use the MouseEnter, MouseHover, MouseLeave events.

Example Code (just tested): The button is "normal" until you move the mouse first time over it, then turns to blue and when you leave the mouse then red Background.

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