簡體   English   中英

使用按鈕向右和向左移動面板單擊C#

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

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

在此輸入圖像描述

在最后兩種方法中,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

(是的,我知道由於協調問題,我們確實破壞了我們的數學測試!)

問題

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

你混合坐標:

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

左按鈕也一樣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM