ボタンの位置を変数に保存しようとしていますが、方法がわかりません。コードはボタンの x と y を示しているため、x と y の両方を別々に保存することはできますか?
Console.WriteLine(button.Location);
<X=100,Y=100>
X値をvar1に、Y値をvar2に保存したい。
Point
1 つまたは 2 つの異なる整数として保存できます。
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)