Display Cursor Coordinates

If you want to display your cursor's coordinates on a form or a control, you can use the MouseMove event assigned to the specific control or form. Once you've added the MouseMove event, all you'll have to do is update the coordinates inside the event.

Bellow is an example that will show you how to display the cursor's coordinates on a form into a label:

public Form1()
{
InitializeComponent();
//sets the size of the form
this.Size = new Size(500, 500);
//initial text to be displayed in the label
label1.Text = "move your mouse over the form";
}

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
//update the label text
label1.Text = "( " + e.X + ", " + e.Y + " )";
}

No comments:

Post a Comment