Reputation: 19
I'm currently trying to get show screen coordinates on my little thing I made.. but I am having issues refreshing the text to the new X, Y values.
int x = Cursor.Position.X;
int y = Cursor.Position.Y;
textBox1.Text = "X: " + x + " Y: " + y + "";
textBox1.Refresh();
That's what I have, but I don't understand why it isn't refreshing? If someone can explain it to me it would be great.
Upvotes: 1
Views: 109
Reputation: 7703
As Patrick Hofman has told you, you must handle the mouse move event. Try this code:
public Form1()
{
InitializeComponent();
this.MouseMove += new MouseEventHandler(Form1_MouseMove);
}
void Form1_MouseMove(object sender, MouseEventArgs e)
{
int x = Cursor.Position.X;
int y = Cursor.Position.Y;
textBox1.Text = "X: " + x + " Y: " + y + "";
}
Upvotes: 1
Reputation: 156978
The text doesn't magically update itself.
You have to handle the appropriate event, for example MouseMove
on your form.
So hook up the event in the constructor;
public Form1()
{
InitializeComponent(); // this is usually already there
this.MouseMove += Form1_MouseMove;
}
Then call your code inside the event handler:
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
int x = Cursor.Position.X;
int y = Cursor.Position.Y;
textBox1.Text = "X: " + x + " Y: " + y + "";
}
Note that this might get slow when there are a lot of events raised. You can build some timer mechanism to update every once in a while, depending on your needs.
Upvotes: 1