使用按钮向右和向左移动面板单击 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);
}
}
(是的,我知道我们 确实 由于坐标问题在某一时刻破坏了我们的数学测试!)
问题
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);
}
左键也一样。
在你的后两种方法中,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
这样的逻辑。
我正在使用 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);
}
}
(是的,我知道我们 确实 由于坐标问题在某一时刻破坏了我们的数学测试!)
问题
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);
}
左键也一样。
在你的后两种方法中,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
这样的逻辑。