0

Possible Duplicate:
How to track mouse X/Y position and print it to a label?

I seem to have run into a road bump with what seems to be a small problem but can't seem to find the answer. This is my first question asked here, and I apologize if I missed the solution to this question!

I have a toolStripStatusLabel that I want to have the current position of the mouse, I was able to get the coordinates doing this:

private void mouseCoordinatesToolStripStatusLabel()
{
    this.toolStripStatusLabel1.Text = MousePosition.ToString();
    this.Refresh();
}

But this has only been able to get me the mouse's position when I load and not when I move my mouse.

Any help would be appreciated, thank you!

Community
  • 1
  • 1
Ryan Taite
  • 789
  • 12
  • 37

1 Answers1

0

This will give you the mouse cursor coordinates:

...
this.toolStripStatusLabel1.Text = Cursor.Position.X.ToString() + " - " + Cursor.Position.Y.ToString();
...

If you want to be updated on mouse move, just bind the MouseMove event from your Form and add previous code. Actually, yours is ok too, you just seem to be missing to bind the the MouseMove event.

danielQ
  • 2,016
  • 1
  • 15
  • 19
  • I was able to get the position with my current code, but I've honestly never used (or at least gotten a grip on) event handlers, would it be possible you could help me by showing me (or even linking me to) how to attach it to a MouseMove event handler? 'private void mouseMove(object sender, MouseEventArgs e) { this.toolStripStatusLabel1.Text = MousePosition.ToString(); this.Refresh(); }' – Ryan Taite Aug 29 '12 at 21:25
  • On your form class constructor write `this.MouseMove +=` then press "TAB" twice and it will attach a new method. Other common option is to set it on the designer, see the properties window, there's an **Events** botton where you can attach to events from your form, doble clicking them. – danielQ Aug 30 '12 at 14:39
  • Handling the event through the property window was exactly what I needed, thank you! In case others see this, what I did was click on my pictureBox in the designer (I was aiming to watch the mouse movement there specifically), Event tab > Scrolled down to MouseMove > Attached it to my mouseMove functions > `private void mouseMove(object sender, MouseEventArgs e) { this.toolStripStatusLabel1.Text = MousePosition.ToString(); this.Refresh(); }` And that worked out great, it adjusts to images loaded to it as well, but the coords are for the whole form. – Ryan Taite Aug 30 '12 at 15:58
  • 1
    Found a solution online and figured out the pictureBox problem just now and figured I should add it here too. My pictureBox1 is set to AutoSize and is in a panel and I'm loading an image through an openFileDialog. First I wrote `Point LocalMousePosition { get; set; }`, then changed the code in my mouseMove function to `private void mouseMove(object sender, MouseEventArgs e) { LocalMousePosition = pictureBox1.PointToClient(Cursor.Position); toolStripStatusLabel1.Text = LocalMousePosition.ToString(); }` and it now shows the mouse coords over the image! – Ryan Taite Aug 30 '12 at 17:00