使用按钮向右和向左移动面板单击C#

时间:2016-01-04 02:15:31

标签: c# .net winforms panel move

我正在使用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);
        }
    }

enter image description here

3 个答案:

答案 0 :(得分:6)

在最后两种方法中,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 , ); 

我还猜测,如果您使用>-y<y的标准,可能需要左右>-x<x的逻辑。

答案 1 :(得分:5)

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

<强>问题

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);
    }
}

答案 2 :(得分:4)

你混合了坐标:

    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);             
    }

左按钮也一样。

相关问题