3

I'm able to get the mouse position when it is within the form. Here is my code-

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    lblXPosition.Text = MousePosition.X.ToString();
    lblYPosition.Text = MousePosition.Y.ToString();
}

But it does not work when the pointer is outside the form. Any suggestion?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Code It
  • 396
  • 1
  • 6

1 Answers1

5

You can do it some other ways. Here is a quick and simple way-

private void timer1_Tick(object sender, EventArgs e)
{
    lblXPosition.Text = MousePosition.X.ToString();
    lblYPosition.Text = MousePosition.Y.ToString();
}

Timer interval time 500 is well enough to do the job. It works even if your pointer is outside of form.

s.k.paul
  • 7,099
  • 28
  • 93
  • 168
  • Indeed @s.k.paul, CodeIt was using using the MouseMove event of the Form1 which is limited to the Form1 and will thus not work when the mouse is outside the Form1. Displaying the position on any other global event was the trick to do it. – Siva Senthil Apr 21 '15 at 05:36
  • @CodeIt I was adding the reason for why the solution worked and the why you experienced the problem in the first place. Thats all. – Siva Senthil Apr 21 '15 at 12:57