taj*_*i01 5 .net c# move panel winforms
我正在使用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);
}
}
Run Code Online (Sandbox Code Playgroud)
在最后两种方法中,x和y的顺序不正确.
要向左移动,你应该减少X:
panel1.Location = new Point(panel1.Location.X - 55, panel1.Location.Y);
Run Code Online (Sandbox Code Playgroud)
要向右移动,你应该增加X:
panel1.Location = new Point(panel1.Location.X + 55, panel1.Location.Y , );
Run Code Online (Sandbox Code Playgroud)
我也想,如果你使用了与标准>-y和向下<y,也许你需要左右这样的逻辑>-x和<x.
(是的,我知道由于协调问题,我们确实破坏了我们的数学测试!)
问题
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);
}
}
Run Code Online (Sandbox Code Playgroud)
您将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);
}
}
Run Code Online (Sandbox Code Playgroud)
第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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1623 次 |
| 最近记录: |