This question already has an answer here:
I have form with few buttons and I want to know what button is under cursor now.
P.S. Maybe it's duplicate, but I can't find answer to this question.
This question already has an answer here:
I have form with few buttons and I want to know what button is under cursor now.
P.S. Maybe it's duplicate, but I can't find answer to this question.
Have a look at
GetChildAtPoint
. You will have to do some extra work if the controls are contained in a container, seeControl.PointToClient
.Maybe
GetChildAtPoint
andPointToClient
is the first idea for most people. I also used it first. But,GetChildAtPoint
doesn't work properly with invisible or overlapped controls. Here's my well-working code and it manages those situations.This will give you the control right under the cursor.
What about defining an on-Mouse-over event in each button which assigns the sender button to a public variable of a button type
You could do it a number of ways:
Listen to the
MouseEnter
event of your form's controls. The "sender" parameter will tell you what control raised the event.Obtain the cursor position using
System.Windows.Forms.Cursor.Location
and map it to your form's coordinates usingForm.PointToClient()
. You can then pass the point toForm.GetChildAtPoint()
to find the control under that point.Andrew