Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move Panel Right and Left with button Click C#

I'm using 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);
        }
    }

enter image description here

like image 701
taji01 Avatar asked Jan 04 '16 02:01

taji01


3 Answers

In your last 2 methods, the order of x and y is incorrect.

To move left, you should decrease X:

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

To Move right, you should increase 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.

like image 190
Reza Aghaei Avatar answered Nov 14 '22 23:11

Reza Aghaei


(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. 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.

Side note: there is a double + in your left button click event too..

Step 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

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.

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);
    }
}
like image 5
Ian Avatar answered Nov 14 '22 22:11

Ian


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.

like image 4
René Vogt Avatar answered Nov 14 '22 21:11

René Vogt