TtT23
TtT23

Reputation: 7040

C# Form not catching any mouse event

I have few controls (Label, Custom Textbox, Datagridview) docked on a form. When I tried to hook the MouseMove event to the individual controls, the event fires perfectly fine but when I tried to hook the event on the form itself, the mousemove event do not respond at all. What could be the possible cause of this?

Edit:

Here is the event hook from resources.cs

this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.LogicSimulationViewerForm_MouseMove);

and here is the function handled on catching the event

private void LogicSimulationViewerForm_MouseMove(object sender, MouseEventArgs e)
        {
           //DOESN'T WORK!!!
        }

Upvotes: 0

Views: 9482

Answers (2)

Me.Name
Me.Name

Reputation: 12544

Winforms events don't bubble up like in WPF or in Html. Therefore if the controls are docked on the form, the form doesn't expose any surface of it's own and it doesn't catch any mouse events. However 'under water', all windows messages (a mouse move is a windows message), do pass the form, so it is possible to catch that message.

edit Tigran has linked to a good example for the use of IMessageFilter that makes creating another example a bit superfluous :)

Upvotes: 3

Tigran
Tigran

Reputation: 62265

The cause of this is that in difference of WPF in WindowsForms the event is "blocked" by the control that handled it (in WPF the event will be pushed to the parent up to the Visual Tree, or in opposite direction: from parent to child).

So to catch that event you need to process it on application level and do not subscribe to single control event.

For more detailed example how to handle it can look here:

How do I capture the mouse mouse move event in my winform application

Upvotes: 2

Related Questions