Search code examples
c#positioncursor

Cursor position C# doesn't work over pictureBox


I would like to get cursor position on form.

Code below works, but not when cursor is positioned over some pictureBox(s).

So I need some help on this.

Thank you !

protected override void OnMouseMove(MouseEventArgs e)
{
    base.OnMouseMove(e);

    Point p = Cursor.Position;

    label1.Text = "x= " + p.X.ToString();
    label2.Text = "y= " + p.Y.ToString();
}

Solution

  • I guess it's because you are overriding only the OnMouseMove - Method of your Form. To capture the mouse move event in your picture box (or any control) use the MouseMove - Event from the control.

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        Point p = Cursor.Position;
    
        label1.Text = "x= " + p.X.ToString();
        label2.Text = "y= " + p.Y.ToString();
    }