C# 如何以编程方式移动窗体?

C# 如何以编程方式移动窗体?,c#,winforms,C#,Winforms,我是这方面的新手,需要你的帮助 目前我有一个表单,表单上有4个按钮(上、下、左、右) 我想将表单移动10个像素到按下的方向。将表单向右移动: private void btnRight_Click(object sender, EventArgs e) { form.Location = new Point(form.Location.X + 10, form.Location.Y); } 将窗体移到左侧: private void btnLeft_Click(object send

我是这方面的新手,需要你的帮助

目前我有一个表单,表单上有4个按钮(上、下、左、右)


我想将表单移动10个像素到按下的方向。

将表单向右移动:

private void btnRight_Click(object sender, EventArgs e) {
     form.Location = new Point(form.Location.X + 10, form.Location.Y);
}
将窗体移到左侧:

private void btnLeft_Click(object sender, EventArgs e) {
     form.Location = new Point(form.Location.X - 10, form.Location.Y);
}  
上移表格:

private void btnUp_Click(object sender, EventArgs e) {
     form.Location = new Point(form.Location.X, form.Location.Y - 10);
}  
向下移动表单:

private void btnDown_Click(object sender, EventArgs e) {
     form.Location = new Point(form.Location.X, form.Location.Y + 10);
}  
编辑:

如果“窗体”是指主窗口,只需将代码中的每个“窗体”替换为“this”。

要单独设置它们,请使用窗体的左(X)或顶(Y)子属性不要尝试隐式设置表示表单位置的点结构的X和Y坐标,因为这包含表单坐标的副本

若要按编程增量更改形状位置,请增加或减少X和Y坐标

  • 左=X
  • 顶部=Y
按钮事件示例: 澄清,例如,
this.Top-=10
是this.Top=this.Top-10的缩写

结果:

你对这件事也不熟悉。请阅读并采取行动←:<代码>this.Left-=10,,→:<代码>this.Left+=10和
Top
也一样。请参阅
位置
属性。谢谢你的回答,吉姆,但是“这”不是指按钮吗?
private void UpButton_Click(object sender, EventArgs e)    
{
    this.Top -= 10;
}

private void DownButton_Click(object sender, EventArgs e)
{
    this.Top += 10;
}

private void RightButton_Click(object sender, EventArgs e)
{
    this.Left += 10;
}

private void LeftButton_Click(object sender, EventArgs e)
{
    this.Left -= 10;
}