【发布时间】:2012-08-16 15:06:47
【问题描述】:
我正在尝试将按钮的位置保存在变量中,但我不知道该怎么做。由于代码显示了按钮的 x 和 y,我还可以分别保存 x 和 y 吗?
Console.WriteLine(button.Location);
<X=100,Y=100>
我希望它将 X 值保存在 var1 中,将 Y 值保存在 var2 中。
【问题讨论】:
标签: c# winforms variables location
我正在尝试将按钮的位置保存在变量中,但我不知道该怎么做。由于代码显示了按钮的 x 和 y,我还可以分别保存 x 和 y 吗?
Console.WriteLine(button.Location);
<X=100,Y=100>
我希望它将 X 值保存在 var1 中,将 Y 值保存在 var2 中。
【问题讨论】:
标签: c# winforms variables location
您可以将其保存为单个 Point 或两个不同的整数:
Point location = button.Location;
int xLocation = button.Location.X;
int yLocation = button.Location.Y;
然后您可以像这样恢复位置:
button.Location = location;
button.Location = new Point(xLocation, yLocation);
注意:Point 是 struct(值类型),因此更改 location 将不会更改 button.Location。换句话说,这不会有任何影响:
Point location = button.Location;
location.X += 100;
你需要这样做:
Point location = button.Location;
location.X += 100;
button.Location = location;
或
button.Location = new Point(button.Location.X + 100, button.Location.Y);
【讨论】:
button.Location.X 会给你 X 值。 button.Location.Y 会给你 Y 值。
所以,是的,您可以单独保存它们。
【讨论】:
尝试:
Point loc = new Point(button.Location.X,button.Location.Y)
【讨论】: