设置窗体位置c#
本文关键字:位置 窗体 设置 | 更新日期: 2023-09-27 18:06:19
我想知道如何设置我的表单位置。我试着做了以下的事情:
this.Location = Point;
或:
Form2.Left = Point.X;
Form2.Top = Point.Y;
Form2.ShowDialog();
这是不工作的。我做错了什么?
private void button1_Click(object sender, EventArgs e)
{
var form2 = new Form();
form2.StartPosition = FormStartPosition.Manual;
form2.Left = 500;
form2.Top = 500;
form2.ShowDialog();
}
以编程方式设置位置,您应该将StartPosition设置为FormStartPosition。手册如下:
Form myform = new Form()
{
Size = new Size(200,200),
StartPosition = FormStartPosition.Manual,
Location = new Point(10,10) // or Cursor.Position if you want to set it to cursor position
}
试试这个:
private void Form_Load(object sender, EventArgs e)
{
this.SetDesktopLocation(x, y);
}
你的第一种方法无论如何都有效。
this.Location = new Point(/*XPosition*/, /*YPosition*/);
你可以这样设置:
form1.Location = new Point(4, 370);
// Point(specify location of x, specify location of y)
// with object initializer
var frmUsers = new FrmUsers
{
StartPosition = FormStartPosition.Manual,
Location = new Point(0, 0)
};
// or
var frmUsers = new FrmUsers();
frmUsers.StartPosition = FormStartPosition.Manual;
frmUsers.Location = new Point(0, 0);