我试图将一个按钮的位置保存在一个变量中,但我不知道该怎么做。由于代码显示了按钮的x和y,我还可以分别保存x和y?在一个变量中放置一个按钮位置值
Console.WriteLine(button.Location);
<X=100,Y=100>
我希望它保存在var1中的X值和在var2中保存Y值。
我试图将一个按钮的位置保存在一个变量中,但我不知道该怎么做。由于代码显示了按钮的x和y,我还可以分别保存x和y?在一个变量中放置一个按钮位置值
Console.WriteLine(button.Location);
<X=100,Y=100>
我希望它保存在var1中的X值和在var2中保存Y值。
您可以将其保存为单个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)