Get cursor position with respect to the control - C#
Use Control.PointToClient to convert a point from screen-relative coords to control-relative coords. If you need to go the other way, use PointToScreen.
You can directly use the Location
property of the MouseEventArgs
argument passed to your event-handler.
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
Text = e.Location.X + ":" + e.Location.Y;
}
The following will give you mouse coordinates relative to your control. For example, this results in (0,0) if mouse is over top left corner of the control:
var coordinates = yourControl.PointToClient(Cursor.Position);