【问题标题】:button over button按钮上的按钮
【发布时间】:2012-05-13 12:47:36
【问题描述】:

我的 winform c# 项目有问题。 在我的项目中,我有两个主要功能,一个在运行时制作按钮,另一个功能允许我在运行时移动表单上的按钮。现在,如果我在其他按钮上有按钮,我该怎么办,所以我制作了替换按钮位置的功能,但如果有人可以帮助我,该功能会出现问题,那就太好了!

public void upandunder(Button cBtn1, Button cBtn2)
    {
        if ((cBtn1.Location.X == cBtn2.Location.X) && (cBtn1.Location.Y == cBtn2.Location.Y))
        {
            int placex = cBtn1.Location.X;
            int placey = cBtn1.Location.Y;
            cBtn1.Location.X = cBtn2.Location.Y;
            cBtn1.Location.Y = cBtn2.Location.Y;
            cBtn2.Location.X = placex;
            cBtn2.Location.Y = placey;

        }
    }

【问题讨论】:

  • 它让我认为 errorError 1 无法修改 'System.Windows.Forms.Control.Location' 的返回值,因为它不是变量 C:\Users\איתן יונה\Desktop\drawsquare2\WindowsFormsApplication1 \Form1.cs 172 17 WindowsFormsApplication1
  • 从将Button x, Button y 更改为Button cBtn1, Button cBtn2 开始,将使您的代码可读...

标签: c# winforms button


【解决方案1】:

它让我觉得errorError 1 不能修改'System.Windows.Forms.Control.Location'的返回值,因为它不是一个变量

正确,Location 属性的返回值不可编辑。根据the documentation

因为Point 类是值类型(Visual Basic 中为Structure,Visual C# 中为struct),所以它是按值返回的,这意味着访问该属性会返回该属性左上角的副本控制。因此,调整从此属性返回的PointXY 属性不会影响控件的LeftRightTopBottom 属性值。要调整这些属性,请单独设置每个属性值,或将Location 属性设置为新的Point

因此,您需要将代码重写为以下内容:
(另外,我强烈建议将参数命名为 xy 以外的名称,因为您正在处理函数中具有 x 和 y 值的坐标...)

public void upandunder(Button btn1, Button btn2)
{
    if ((btn1.Location.X == btn2.Location.X) && (btn1.Location.Y == btn2.Location.Y))
    {
        Point originalLocation = btn1.Location;
        btn1.Location = btn2.Location;
        btn2.Location = originalLocation;
    }
}

甚至更好,只需比较Location 属性(Point 结构overloads the == operator)返回的两个Point 值:

public void upandunder(Button btn1, Button btn2)
{
    if (btn1.Location == btn2.Location)
    {
        Point originalLocation = btn1.Location;
        btn1.Location = btn2.Location;
        btn2.Location = originalLocation;
    }
}

当然,我看不出它是如何完成任何事情的。首先,您检查按钮是否位于彼此之上(具有完全相同的 x 和 y 坐标),然后如果有,则交换它们的位置。它们已经相同的位置——你在执行交换代码之前测试了它。

根据您的函数名称(upandunder,按照标准 .NET 命名约定应为 UpAndUnder)判断,您似乎希望更改按钮的 Z 顺序。如果是这种情况,那么您应该调用按钮控件的BringToFrontSendToBack 方法。

【讨论】:

  • 如果按钮正在按下其他按钮,他们会改变位置吗?
  • 该功能不起作用,它只是将第一个按钮放在第二个按钮下方,如果 x 有 x.location = 5,6;并且 y 有 y.loction = 9,6;如果 tuch 然后 x.loction = 9,6 和 y.loction = 5,6 则需要切换位置;
  • @eit 这些按钮不记得他们以前在哪里,他们只知道他们在哪里现在。正如我在回答中解释的那样,您的 if 语句检查按钮当前是否位于彼此之上,如果是,则交换它们的位置。这实际上什么也没做。您需要将按钮的位置保存在成员变量中,以便以后根据需要重新使用。
  • 好的,现在我想我知道该怎么做了,如果它是 posibal 的话,我会保持联系 :)?
【解决方案2】:

控件上的Location 属性返回一个点。 Point 结构具有您正在使用的 X 和 Y 值。我认为您不想直接访问它们,而是想提供新的位置点。

试试这个(它在我的机器上工作)

public void UpAndUnder(Button cBtn1, Button cBtn2)
{
    if (cBtn1.Location == cBtn2.Location.Y)
    {                
        Point oldPoint = new Point(cBtn1.Location.X, cBtn1.Location.Y);
        cBtn1.Location = new Point(cBtn2.Location.X, cBtn2.Location.Y);
        cBtn2.Location = oldPoint;
    }
}

【讨论】:

    【解决方案3】:

    如果您想将一个按钮放在另一个按钮上,只需调用

    button1.BringToFront();
    

    这将改变 button1 的 Z 顺序并将其置于所有其他控件之上。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-02
      相关资源
      最近更新 更多