【问题标题】:Move Panel Right and Left with button Click C#使用按钮左右移动面板单击 C#
【发布时间】:2016-04-07 15:14:08
【问题描述】:

我正在使用 WinForms。在我的表单中,我有一个带有可移动面板的按钮的面板。例如,向上和向下按钮可以向上或向下移动面板。我很难用相应的按钮左右移动面板。我做错了什么?

    private void Up_btn_Click(object sender, EventArgs e)
    {
        if (panel1.Location.Y > -2000) 
        {
            panel1.Location = new Point(panel1.Location.X, panel1.Location.Y - 80);        
        }
    }

    private void Down_btn_Click(object sender, EventArgs e)
    {
        if (panel1.Location.Y < 720) 
        {
            panel1.Location = new Point(panel1.Location.X, panel1.Location.Y + 80);
        }
    }

    private void Left_btn_Click(object sender, EventArgs e)
    {
        if (panel1.Location.X < 720) 
        {
            panel1.Location = new Point(panel1.Location.Y , panel1.Location.X + +55);             
        }
    }

    private void Right_btn_Click(object sender, EventArgs e)
    {
        if (panel1.Location.X < 720) 
        {
            panel1.Location = new Point(panel1.Location.Y, panel1.Location.X -55);
        }
    }

【问题讨论】:

  • 有什么问题?
  • 在您的最后 2 个位置中,x 和 y 的顺序不正确。
  • 这一行 panel1.Location = new Point(panel1.Location.Y , panel1.Location.X + +55);那里有双 + 标志。
  • 不客气 :) 此外,如果您使用 &gt;-y 的 up 条件和 &lt;y 的 down 条件,您可能需要左右 &gt;-x&lt;x 的这种逻辑
  • @taji01 欢迎您。很高兴答案对您有任何帮助。正如我在答案中提到的那样,我们知道我们确实有时也因为这个问题而破坏了我们的数学测试......;)

标签: c# .net winforms panel move


【解决方案1】:

在你的最后 2 个方法中,x 和 y 的顺序不正确。

要向左移动,你应该减少X

panel1.Location = new Point(panel1.Location.X - 55, panel1.Location.Y);

要向右移动,你应该增加X

panel1.Location = new Point(panel1.Location.X + 55,  panel1.Location.Y , ); 

我也猜想如果你使用 &gt;-y 的 up 条件和 &lt;y 的 down 条件,可能你需要左右 &gt;-x&lt;x 的这种逻辑。

【讨论】:

    【解决方案2】:

    (是的,我知道我们确实由于坐标问题而破坏了我们的数学测试!)

    问题

    Point() 始终是 (x,y) 坐标。在您的代码中:

    private void Left_btn_Click(object sender, EventArgs e)
    {
        if (panel1.Location.X < 720) 
        {
            panel1.Location = new Point(panel1.Location.Y , panel1.Location.X + +55);             
        }
    }
    
    private void Right_btn_Click(object sender, EventArgs e)
    {
        if (panel1.Location.X < 720) 
        {
            panel1.Location = new Point(panel1.Location.Y, panel1.Location.X -55);
        }
    }
    

    你把X坐标和Y值放在一起,反之亦然。

    旁注:在您的左键单击事件中也有一个双 +..

    第 1 步

    首先,做相反的事情:

    private void Left_btn_Click(object sender, EventArgs e)
    {
        if (panel1.Location.X < 720) 
        {
            panel1.Location = new Point(panel1.Location.X + 55 , panel1.Location.Y);             
        }
    }
    
    private void Right_btn_Click(object sender, EventArgs e)
    {
        if (panel1.Location.X < 720) 
        {
            panel1.Location = new Point(panel1.Location.X - 55, panel1.Location.Y);
        }
    }
    

    第 2 步

    其次,看看左右是不是你想要的。请注意,向左移动意味着我们减小 X,向右移动我们增加 X。

    不应该这样吗?

    private void Left_btn_Click(object sender, EventArgs e) //The name is Left
    {
        if (panel1.Location.X < 720) 
        {
            panel1.Location = new Point(panel1.Location.X - 55 , panel1.Location.Y);             
        }
    }
    
    private void Right_btn_Click(object sender, EventArgs e) //The name is Right
    {
        if (panel1.Location.X < 720) 
        {
            panel1.Location = new Point(panel1.Location.X + 55, panel1.Location.Y);
        }
    }
    

    【讨论】:

    • 双+没有问题,因为它表示数字是正数。
    • @PhiterFernandes 是的,这只是一个旁注。
    • 使用panel1.Location.X + 55你的左键将面板向右移动!
    【解决方案3】:

    你混合了坐标:

        if (panel1.Location.X < 720) 
        {
            panel1.Location = new Point(panel1.Location.Y , panel1.Location.X + 55);             
        }
    

    应该是

        if (panel1.Location.X < 720) 
        {
            panel1.Location = new Point(panel1.Location.X + 55, panel.Location.Y);             
        }
    

    左键也是如此。

    【讨论】:

      猜你喜欢
      • 2022-11-25
      • 1970-01-01
      • 2021-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多