2

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?

kfirba
  • 5,231
  • 14
  • 41
  • 70
  • You have to use a timer and check the mouse position or this solution [determine if mouse has left user control](http://stackoverflow.com/a/425361/719186) – LarsTech Feb 12 '13 at 16:35

2 Answers2

1

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
SysDragon
  • 9,692
  • 15
  • 60
  • 89
  • Using this solution, you probably need to capture the mouse to ensure that you get the mouse movements if the user moves the mouse very quickly off of the form, especially if the panel is very close to the edge of the form. – Steven Doggart Feb 12 '13 at 16:22
  • how do you apply that to a programatically added `Panel` that contains `Labels` and `Picturebox` ? – kfirba Feb 12 '13 at 16:24
  • Just adding the handler with `AddHandler` (for each Panel) to the `MouseEnter` function and using the `sender` in the function to get the Panel. And in the Form `MouseMove` event, you have to check the controls manually in `Me.Controls`. – SysDragon Feb 12 '13 at 16:33
  • well, that's a solution, but seems to be too complicated – kfirba Feb 12 '13 at 16:35
1

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
Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
  • how do you apply that to a programatically added `Panel` that contains `Labels` and `Picturebox` ? There are many panels that I add – kfirba Feb 12 '13 at 16:25
  • Instead of programatically adding the Panel, you'd need to programatically add your custom type of panel. – Steven Doggart Feb 12 '13 at 16:27
  • @StevenDoggart, do you have any other suggestion ? This works only If you enter in panel's empty space first - If you enter in any other control in Panel first, this code doesn't work. – LuckyLuke82 Dec 10 '16 at 13:10
  • @LuckyLuke82 Hmm. Good point. Well, you could add the same mouse-enter handler for the panel as well as all of its children controls. There may be a "simpler" way to do it by overriding the `WndProc` method, but I wouldn't know off the top of my head. – Steven Doggart Dec 12 '16 at 00:48
  • @StevenDoggart, thanks for info. But I solved It with handling all Panel and children control's Mouse-Enter/Leave events, without using any inherited control. It works quite good, although I my case sometimes It get stucked (I have 3 panels tight together and sometimes 2 of them remain same backolor - when fast mouse moving). – LuckyLuke82 Dec 12 '16 at 07:22