在 C# 中更新标签位置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/968728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:28:39  来源:igfitidea点击:

Update label location in C#?

c#winformslabelruntime

提问by avaleske

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.

我有一个返回一个值的方法,我希望这个值成为 Windows 窗体应用程序中标签的新位置。但有人告诉我标签的位置不是变量。objectA 是标签的名称。

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

how do I do this?

我该怎么做呢?

采纳答案by Julien Poulin

Use the Leftproperty to change X coordinate of a Label

使用该Left属性更改一个的 X 坐标Label

objectA.Left = 100;

回答by Thomas Levesque

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 :

Location 属性属于 Point 类型,这是一种值类型。因此,该属性返回位置值的副本,因此在此副本上设置 X 不会对标签产生影响。编译器会看到并生成错误,以便您可以修复它。你可以这样做:

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

(the call to Refresh is useless)

(调用 Refresh 没用)

回答by 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.

位置不是变量,只是一个公共属性。除非您有更新父级的事件,否则通过属性更改变量是一个坏主意。

回答by RomanT

This works to me

这对我有用

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

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

您甚至不需要调用 Refresh 或 SuspendLayout 等。

so this should help you

所以这应该可以帮助你

this.label1.Location = new Point((int)A.position, (int)A.otherpos);

回答by Alynuzzu

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

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

回答by Miguel Angelo

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;