kfirba
kfirba

Reputation: 5441

Set Panel backcolor when hover

I'm creating a Panel with labels and pictureboxes in it programitacally.

What I want to do it, whenever the mouse Hovers the Panel the Panel backcolor will set to Steelblue and whenever MouseLeave occurs, the backcolor sets back to Transparent

My problem is, whenever I hover a child of the Panel such as a Label or a Picturebox I lose the backcolor, because the code considers that as MouseLeave event of the Panel.

Therefore, I have tried to do a function that whenever I Hover a child of the Panel it will set the Panel backcolor to SteelBlue.

Now the problem is, that the BackColor flickers because whenever I hover a Label or a Picturebox it considers that as a MouseLeave event of the Panel

How do I make the BackColor of the Panel remain the same until I actually Leave the Panel boundaries?

Upvotes: 2

Views: 2917

Answers (2)

Steven Doggart
Steven Doggart

Reputation: 43743

I don't know of a really easy way of doing this. The best way is to create a new control that inherits from the Panel control. If you do that, then you can override the OnMouseLeave method, like this:

Protected Overrides Sub OnMouseLeave(e As EventArgs)
    If Not Me.ClientRectangle.Contains(Me.PointToClient(Control.MousePosition)) Then
        MyBase.OnMouseLeave(e)
    End If
End Sub

Upvotes: 1

SysDragon
SysDragon

Reputation: 9888

You can use MouseMove event and check the position on the form:

Sub Panel1_MouseEnter(sender As Object, e As EventArgs) Handles Panel1.MouseEnter
    Panel1.BackColor = Color.SteelBlue
End Sub

Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
    If Not Panel1.Bounds.Contains(e.Location) Then
        Panel1.BackColor = SystemColors.Control
    End If
End Sub

Upvotes: 1

Related Questions