Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update label location in C#?

I have a method that returns a value, and I want this value to be the new location of a label in a windows form application. but I'm being told that a label's location is not a variable. objectA is the name of the label.

objectA.Location.X = (int)A.position;
objectA.Refresh();

how do I do this?

like image 705
avaleske Avatar asked Jun 09 '09 07:06

avaleske


3 Answers

Use the Left property to change X coordinate of a Label

objectA.Left = 100;
like image 150
Julien Poulin Avatar answered Sep 21 '22 22:09

Julien Poulin


the Location property is of type Point, which is a value type. Therefore, the property returns a copy of the location value, so setting X on this copy would have no effect on the label. The compiler sees that and generates an error so that you can fix it. You can do that instead :

objectA.Location = new Point((int)A.position, objectA.Location.Y);

(the call to Refresh is useless)

like image 42
Thomas Levesque Avatar answered Sep 25 '22 22:09

Thomas Levesque


This works to me

this.label1.Location = new Point(10, 10);

You even do not need to call Refresh or SuspendLayout etc.

so this should help you

this.label1.Location = new Point((int)A.position, (int)A.otherpos);
like image 24
RomanT Avatar answered Sep 22 '22 22:09

RomanT