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