Update label location in C#?

僤鯓⒐⒋嵵緔 提交于 2019-11-29 01:36:21

Use the Left property to change X coordinate of a Label

objectA.Left = 100;

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)

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);

objectname.Location = System.Drawing.Point(100,100);

You can only set properties of structs if you have a direct reference to that struct:

Point loc = objectA.Location;
loc.X = (int)A.position;
objectA.Location = loc;
Alin Vasile
objectA.Location = new Point((int)A.position, objectA.Location.Y);
objectA.Refresh();

Location is no a variable, just a public Property. Changing variables through properties is a bady idea unless you have events that update the parent.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!