0

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!!!
        }
TtT23
  • 6,876
  • 34
  • 103
  • 174
  • It only fires when you click the form itself, not its elements. – nxu Jun 26 '12 at 06:54
  • I'm talking about mousemove not mousedown – TtT23 Jun 26 '12 at 06:54
  • You should create **minimal** example code that doesn't work and update your question with it. No one will be able to help you without source code. – torvin Jun 26 '12 at 06:57
  • 3
    I'm honestly not sure why you would need a **minimal** example code for something that is **ridiculously trivial** but I'm not the one to complain so I will edit my question. – TtT23 Jun 26 '12 at 07:01

2 Answers2

3

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 :)

Community
  • 1
  • 1
Me.Name
  • 12,259
  • 3
  • 31
  • 48
2

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

Community
  • 1
  • 1
Tigran
  • 61,654
  • 8
  • 86
  • 123