By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,515 Members | 1,061 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,515 IT Pros & Developers. It's quick & easy.

Panel and MouseEnter / MouseLeave

P: n/a
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

Nov 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.

Nov 20 '05 #2

P: n/a
Hi Peter,

Thanks!!!

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

Thanks agan.

M O J O

Peter Huang wrote:
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.


Nov 20 '05 #3

P: n/a
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?
Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.