user3408531
user3408531

Reputation:

Changing the location of a Label

I have tried to programmatically set the location of a label but the compiler says it is not a variable,

label1.Location.X = 200;// shows an error

is there any trick I could use to move labels around which does not include hiding and showing controls.

The technology is visual c# not WPF.

I'm looking for a way to move some text on the form,I separated the characters of the text, label for each, and also the mathematic formula

(x',y')=center;x'=200,y'=200,r=100
(x,y)=point on the circle.
sqr(x-x')+sqr(y-y')=sqr(r) => sqr(x-200)+sqr(y-200)=10000 => sqr(x)-400x+sqr(y)-400y+70000=0 =>  
x1=(400+sqrt(160000-4sqr(y)+1600y-280000))/2 ; 
x2=(400-sqrt(160000-4sqr(y)+1600y-280000))/2

Also I am thinking of using threads to refresh the form and create the motion, it is just some ideas I am trying to work this out since you are asking.

many thanks.

Upvotes: 1

Views: 3768

Answers (2)

Hans Passant
Hans Passant

Reputation: 942358

Every .NET programmer makes this mistake at least once. The Location property is a Point, a value type. When you retrieve its value then you get a copy, like value types behave. You are updating the X property of that copy, the compiler can tell that this is not what you had in mind.

You have to assign a Point to update the property value:

  label1.Location = new Point(200, label1.Location.Y);

Or use the property that was made to avoid having to write this code:

  label1.Left = 200;

Upvotes: 4

YoryeNathan
YoryeNathan

Reputation: 14532

To properly change the location of the label, you must set the Location to a value (rather than the X property of the Location).

lbl.Location = new Location(20, 50);

If you wish to preserve the Y coordinates, for example, you can do:

lbl.Location = new Location(20, lbl.Location.Y);

Upvotes: 2

Related Questions