【问题标题】:placing a button location value in a variable将按钮位置值放在变量中
【发布时间】: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


    【解决方案1】:

    您可以将其保存为单个 Point 或两个不同的整数:

    Point location = button.Location;
    int xLocation = button.Location.X;
    int yLocation = button.Location.Y;
    

    然后您可以像这样恢复位置:

    button.Location = location;
    button.Location = new Point(xLocation, yLocation);
    

    注意:Pointstruct(值类型),因此更改 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);
    

    【讨论】:

      【解决方案2】:

      button.Location.X 会给你 X 值。 button.Location.Y 会给你 Y 值。

      所以,是的,您可以单独保存它们。

      【讨论】:

        【解决方案3】:

        尝试:

        Point loc = new Point(button.Location.X,button.Location.Y)
        

        【讨论】:

          猜你喜欢
          • 2015-08-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-12-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多