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);
}
}
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
.
(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);
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With