0

I've been looking for this thing... It should be working yet it is not. There must be something I don't get understand or that I'm missing. It's quite a simple problem but I can't seem to solve it.

I got Panel1 and Panel2 as shown in this picture. enter image description here

I want to catch when mouse is over Panel2 within Panel1 MouseLeave event. My code goes like this :

Private Sub Panel1_MouseLeave(sender As Object, e As EventArgs) Handles Panel1.MouseLeave

    If sender.ClientRectangle.Contains(PointToClient(Control.MousePosition)) Then
        For Each ctrl As Object In sender.controls
            If ctrl.ClientRectangle.Contains(PointToClient(Control.MousePosition)) Then Exit Sub
        Next
        If Not IsNothing(sender.BackgroundImage) Then sender.BackgroundImage = Nothing
    End If

End Sub

Private Sub Panel2_MouseLeave(sender As Object, e As EventArgs) Handles Panel2.MouseLeave

    If Not sender.ClientRectangle.Contains(PointToClient(Control.MousePosition)) Then
        If Not IsNothing(sender.BackgroundImage) Then sender.BackgroundImage = Nothing
    End If

End Sub

I'm successfully getting into the first if, but the 2nd one within the For Each just never equals true. So I thought maybe there was a problem with the 2nd panel, so I tried placing the same code for Panel2 MouseLeave, but it's working just fine.

I really need this code to work for a big control flickering problem I'm having.

Senerio
  • 35
  • 1
  • 9
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Jun 18 '15 at 01:50
  • What is the reason you need to use MouseLeave? Using both MouseEnter events will give you more consistent results. See [this previous example](http://stackoverflow.com/questions/26323105/different-panels-mouseenter-and-mouseleave-conflict-on-event-order/26330014#26330014). – Justin Ryan Jun 18 '15 at 04:17
  • Because several panels will be placed on runtime constructed by a class. Moving the mouse over them constantly triggers the events, which makes the panel flicker enormously. Furthermore, I need to also take into account that the mouse is completely outside the Panel1. Right now, the panels inside the panel count as "outside", and that's the problem. – Senerio Jun 18 '15 at 04:45
  • Try using a timer to track the mouse position to see if it's within the bounds of the parent panel. – LarsTech Jun 18 '15 at 22:39
  • 1
    The PointToClient() call is wrong, you are using the form's method. Wrong client. – Hans Passant Jun 18 '15 at 22:40
  • Never mind got it! Thanks for the hint! Simply had to change Panel1.PointToClient(Control.MousePosition) and ctrl.PointToClient(Control.MousePosition) – Senerio Jun 18 '15 at 22:48

1 Answers1

0

Thanks to Hans Passant for the hint. I just had to call the PointToClient with the right control :

Private Sub Panel1_MouseLeave(sender As Object, e As EventArgs) Handles Panel1.MouseLeave

    If sender.ClientRectangle.Contains(Panel1.PointToClient(Control.MousePosition)) Then
        For Each ctrl As Object In sender.controls
            If ctrl.ClientRectangle.Contains(ctrl.PointToClient(Control.MousePosition)) Then Exit Sub
        Next
        If Not IsNothing(sender.BackgroundImage) Then sender.BackgroundImage = Nothing
    End If

End Sub
Senerio
  • 35
  • 1
  • 9