Determine the Control under the mouse cursor

  • Thread starter Thread starter Andrea V.F.
  • Start date Start date
A

Andrea V.F.

How can I determine the control under the mouse cursor when a user
muves the mouse?? For example if the user moves the cursor over a
textbox i would receive the name of that textbox without using the
"MouseMove" event for that textbox.

Thanks
 
Andrea V.F. said:
How can I determine the control under the mouse cursor when a user
muves the mouse?? For example if the user moves the cursor over a
textbox i would receive the name of that textbox without using the
"MouseMove" event for that textbox.

Take a look at the form's 'GetChildAtPoint' method. You can determine the
current cursor position using 'Cursor.Position'.
 
Your suggestion works if a component is on a Form, but not if the
component is on a Panel (as in my case).

Thanks for help.
 
Andrea said:
Your suggestion works if a component is on a Form, but not if the
component is on a Panel (as in my case).

If the control returned from Form.GetControlAtPoint is a container
control, then you need to call *its* GetControlAtPoint method, and so
on recursively until you end up with a control that isn't a container,
or Nothing, which means you are on a blank part of the container. Make
sure to transform your Point appropriately on the way down.

It might be easier to hook up all your form's controls' MouseMove event
to a single event handler.
 
if I call the GetControlAtPoint of the panel I receive always Nothing
even if I'm on a control into the panel.

What do you mean with "Make
sure to transform your Point appropriately on the way down."
 
Andrea V.F. said:
if I call the GetControlAtPoint of the panel I receive always Nothing
even if I'm on a control into the panel.

What do you mean with "Make
sure to transform your Point appropriately on the way down."

'Control.PointToClient' et al. The control's 'GetControlAtPoint' method
expects control coordinates, not screen coordinates.
 
Herfried said:
'Control.PointToClient' et al. The control's 'GetControlAtPoint' method
expects control coordinates, not screen coordinates.

I was gonna say that and I had a nice sample ready, but my sample
doesn't work! I have a nice recursive routine that takes screen
coordinates and returns the control in that position - BUT - when do I
call it? Form.MouseMove is no use as it isn't called when not
*directly* on the form; and I think we wanted to avoid hooking to every
MouseMove event?

Confused.
 
Back
Top