Determine the Control under the mouse cursor

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
 
H

Herfried K. Wagner [MVP]

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'.
 
A

Andrea V.F.

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.
 
L

Larry Lard

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.
 
A

Andrea V.F.

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."
 
H

Herfried K. Wagner [MVP]

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.
 
L

Larry Lard

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top