Panel and MouseEnter / MouseLeave

M

M O J O

Hi,

I have a panel on my form.

I want to detect when the user enters my panel with his mouse and when
he leaves my panel.

If the user hovers a control inside my panel, I get the mouseleave,
which is understandable.

But how do I make the MouseLeave event fire only when the user moves the
cursor outside the panel and not when he hovers child controls of the panel?

Thanks in advance!

M O J O
 
P

Peter Huang

Hi Mojo,

Thanks for using Microsoft MSDN Managed Newsgroup. My name is Peter, and I
will be assisting you on this issue.

First of all, I would like to confirm my understanding of your issue.
From your description, I understand that you do not want the MouseLeave
event was fired when the mouse enter the control on the panel, by default
this will be fired.
Have I fully understood you? If there is anything I misunderstood, please
feel free to let me know.

I think you can derived a cusomized Panel and override the OnMouseMove
event.
Here I write a sample for you.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Panel1 = New MyPanel
Dim textBox1 As New TextBox
Dim label1 As New Label
' Initialize the Panel control.
panel1.Location = New Point(56, 72)
panel1.Size = New Size(264, 152)
' Set the Borderstyle for the Panel to three-dimensional.
panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
' Initialize the Label and TextBox controls.
label1.Location = New Point(16, 16)
label1.Text = "label1"
label1.Size = New Size(104, 16)
textBox1.Location = New Point(16, 32)
textBox1.Text = ""
textBox1.Size = New Size(152, 20)
' Add the Panel control to the form.
Me.Controls.Add(panel1)
' Add the Label and TextBox controls to the Panel.
panel1.Controls.Add(label1)
panel1.Controls.Add(textBox1)
End Sub
Private Sub Panel1_MouseLeave(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Panel1.MouseLeave
MsgBox("Mouse Leave")
End Sub
End Class
Public Class MyPanel
Inherits System.Windows.Forms.Panel
Protected Overrides Sub OnMouseLeave(ByVal e As System.EventArgs)
Dim clientPoint As Point = PointToClient(Cursor.Position)
If Me.DisplayRectangle.Contains(clientPoint) Then
Return
End If
MyBase.OnMouseLeave(e)
End Sub
End Class

Does this answer your question?
If you have any concern on this issue, please post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

M O J O

Hi Peter,

Thanks!!!

It was exactly what I was looking for, but couldn't figure it out. :blush:)

Thanks agan.

M O J O
 
C

Cor

Hi Mojo,

The solution from Peter is nice of course, it gives a good method how to
catch the possition of the mouse.

For your question you can also use a switch in the mouseenter en mouseleave
event.
A sample with colors.

\\\
Private Sub Panel1_MouseLeave(ByVal sender _
As Object, ByVal e As System.EventArgs) Handles Panel1.MouseLeave
Me.Panel1.BackColor = Color.Blue
End Sub

Private Sub Panel1_MouseEnter(ByVal sender _
As Object, ByVal e As System.EventArgs) Handles Panel1.MouseEnter
Me.Panel1.BackColor = Color.Red
End Sub
///
I hope this helps a little bit?

Cor
I have a panel on my form.

I want to detect when the user enters my panel with his mouse and when
he leaves my panel.

If the user hovers a control inside my panel, I get the mouseleave,
which is understandable.

But how do I make the MouseLeave event fire only when the user moves the
cursor outside the panel and not when he hovers child controls of the
panel?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top