Mouse Move on the Panel Control

I want to catch the mouse move on a panel. I use the following Sub

PrivateSub Panel1_MouseMove(ByVal senderAsObject,ByVal eAs System.Windows.Forms.MouseEventArgs)Handles Panel1.MouseMove

EndSub

But when the mouse is on another control of the panel ( lets say a label ) , the program doesn't trigger the sub. I have to write another mosemove sub for the child control or I have to add handles behind the other sub . But I don't want to do it for each control.

Is there a way to solve this issue?

[1227 byte] By [AyhanYerli] at [2007-12-24]
# 1

Below is a code snippet that shows how you can direct all mousemove events with a single mousemove handler. It's real primitive in that you will have to add logic to translate coordinates of child controls to the disired parent coordinates. This little demo will display the x,y pos of mouse relative to the control the mouse is moving over.

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Me.AddHandlers(Me)

End Sub

Private Sub AddHandlers(ByVal CtrlIn As Control)

For Each Item As Control In CtrlIn.Controls

AddHandler Item.MouseMove, AddressOf MyMouseMove

If Item.Controls.Count > 0 Then

AddHandlers(Item)

End If

Next

End Sub

Private Sub MyMouseMove(ByVal Sender As Object, ByVal e As MouseEventArgs)

'you will have to write code to translate X-Y coordinates relative to the

'parent container's coordinates if needed

Me.Text = e.X & "::" & e.Y

End Sub

End Class

Ibrahim Malluf

IbrahimY at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2
You want to capture the mouse so any mouse messages are directed to the panel control, not the control that the mouse cursor is currently hovering over. The code below demonstrates the technique. Drop a label, panel with a dark background and a button on a form, then paste this code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Panel1.Capture = True
End Sub

Private Sub Panel1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseMove
Label1.Text = String.Format("{0}, {1}", e.X, e.Y)
End Sub

It is up to you to decide when to cancel the capture...

nobugz at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...