我正在使用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); } }
在最后两种方法中,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
和向下>-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); } }
您将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); } }