简体   繁体   English

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

[英]Move Panel Right and Left with button Click C#

I'm using WinForms. 我正在使用WinForms。 In my Form I have a Panel with buttons that move the panel. 在我的表格中,我有一个带按钮的面板可以移动面板。 For example the Up and Down button move the panel up or down. 例如,向上和向下按钮可向上或向下移动面板。 I'm having difficulties moving the panel left and right with the corresponding buttons. 我在使用相应的按钮左右移动面板时遇到困难。 What i'm i doing wrong? 我做错了什么?

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

在此输入图像描述

In your last 2 methods, the order of x and y is incorrect. 在最后两种方法中,x和y的顺序不正确。

To move left, you should decrease X : 要向左移动,你应该减少X

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

To Move right, you should increase X : 要向右移动,你应该增加X

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

I also guess if you are using up criteria with >-y and down with <y , probably you need such logic for left and right >-x and <x . 我还猜你是否正在使用>-y<y ,可能你需要左右这样的逻辑>-x<x

(Yes, I know that we did spoil our math tests at one point or another due to coordinate issue!) (是的,我知道由于协调问题,我们确实破坏了我们的数学测试!)

Problem 问题

Point() is always (x,y) coordinate. Point()始终是(x,y)坐标。 In your code: 在你的代码中:

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

You put X coordinate with Y value and vice versa. 您将X坐标与Y值相对应,反之亦然。

Side note: there is a double + in your left button click event too.. 旁注:你的左键点击事件也有一个双+ ..

Step 1 步骤1

First, do the reverse: 首先,反过来:

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

Step 2 第2步

Secondly, see if left and right is what you intended. 其次,看看左右是否是你想要的。 Note that moving left means we decrease our X and moving right we increase our X. 请注意,向左移动意味着我们减少X并向右移动我们增加X.

Should it not be done this way? 不应该这样做吗?

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

You mixed the coordinates: 你混合坐标:

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

should be 应该

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

And the same for the left button. 左按钮也一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM