Move the panel left and right using the button. Press C #

I am using WinForms. In my form, I have a panel with buttons that move the panel. For example, the Up and Down buttons move the panel up or down. I'm having difficulty moving the panel left and right using the appropriate buttons. What am 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

+5
source share
3 answers

(Yes, I know that we messed up our math tests at some point due to a problem with the coordinates!)

Problem

Point() 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 an X coordinate with a Y value and vice versa.

Side note: there is a double + in the left mouse click event.

Step 1

First do the opposite:

 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, look what you are looking for if you want. Note that moving to the left means that we decrease our X and move to the right, we increase X.

Should this not be done?

 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); } } 
+5
source

In your last two methods, the order of x and y is wrong.

To move left you must decrease X :

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

To move to the right, you must increase X :

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

I also suggest that if you use criteria with >-y and down with <y , you probably need this logic for left and right >-x and <x .

+6
source

You mixed the coordinates:

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

it should be

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

And the same goes for the left button.

+4
source

Source: https://habr.com/ru/post/1239756/


All Articles