我有一个返回值的方法,我希望这个值成为 Windows 窗体应用程序中标签的新位置。但有人告诉我标签的位置不是变量。objectA 是标签的名称。
objectA.Location.X = (int)A.position;
objectA.Refresh();
我该怎么做呢?
使用该Left
属性更改 a 的 X 坐标Label
objectA.Left = 100;
Location 属性是 Point 类型,它是一个值类型。因此,该属性返回位置值的副本,因此在该副本上设置 X 不会对标签产生影响。编译器会看到并生成一个错误,以便您修复它。你可以这样做:
objectA.Location = new Point((int)A.position, objectA.Location.Y);
(对 Refresh 的调用是没用的)
这对我有用
this.label1.Location = new Point(10, 10);
您甚至不需要调用 Refresh 或 SuspendLayout 等。
所以这应该可以帮助你
this.label1.Location = new Point((int)A.position, (int)A.otherpos);
objectname.Location = System.Drawing.Point(100,100);
objectA.Location = new Point((int)A.position, objectA.Location.Y);
objectA.Refresh();
位置不是变量,只是一个公共属性。除非您有更新父级的事件,否则通过属性更改变量是一个坏主意。
如果您直接引用该结构,则只能设置结构的属性:
Point loc = objectA.Location;
loc.X = (int)A.position;
objectA.Location = loc;