C# 调用 Form.Show() 时设置表单的位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17369159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:20:01  来源:igfitidea点击:

Setting form's location when calling Form.Show()

c#.netwindowsvisual-studio-2012

提问by Robin

I'm trying to set the location of a form when calling it by .Show(). The problem is that because I'm using .Showinstead of .ShowDialogthe StartPosition value does not work. I can't use the .Showdialogsince I want the program to do work in the background while showing the form.

我试图在调用表单时设置表单的位置.Show()。问题是因为我使用.Show而不是.ShowDialogStartPosition 值不起作用。我不能使用 ,.Showdialog因为我希望程序在显示表单时在后台工作。

When I'm creating the form I set it's location to a fixed value:

创建表单时,我将其位置设置为固定值:

using (ConnectingForm CF = new ConnectingForm())
{
    CF.Show();
    CF.Location = new Point(this.ClientSize.Width / 2, this.ClientSize.Height / 2);
}

But when I run the Code the form places itself on different positions each time I start it.

但是当我运行代码时,每次我启动它时,表单都会将自己置于不同的位置。

Any solutions? (The location is never set anywhere else by my code)

任何解决方案?(我的代码从未在其他任何地方设置该位置)

采纳答案by Robin

With some help from other threads I found a working solution:

在其他线程的帮助下,我找到了一个可行的解决方案:

    using (ConnectingForm CF = new ConnectingForm())
    {
        CF.StartPosition = FormStartPosition.Manual;
        CF.Show(this);
        ......
    }

On the new form's load event:

在新表单的加载事件上:

    private void ConnectingForm_Load(object sender, EventArgs e)
    {
        this.Location = this.Owner.Location;
        this.Left += this.Owner.ClientSize.Width / 2 - this.Width / 2;
        this.Top += this.Owner.ClientSize.Height / 2 - this.Height / 2;
    }

(I'm no expert so please correct me if I'm wrong) Here is how I interpret the problem and the solution: The problem from the beginning was that the first form's (MainForm) Startup Position was set to Windows Default Location which varies when you start up the form. When I then called the new form (Connecting form), it's location was not relative to it's parent's location, but the location (0, 0) (top lef corner of the screen). So what I was seeing was the MainForms position changing, which made it look like the Connecting Form's position was moving. So the solution to this problem was basically to first set the new form's location to the Main Form's location. After that I was able to set the location to be the center of the MainForm.

(我不是专家,所以如果我错了,请纠正我)这是我如何解释问题和解决方案:从一开始的问题是第一个表单的(MainForm)启动位置设置为 Windows 默认位置,该位置会有所不同当您启动表单时。当我然后调用新表单(连接表单)时,它的位置不是相对于它的父位置,而是位置 (0, 0)(屏幕的左上角)。所以我看到的是 MainForms 的位置在变化,这使得连接表单的位置看起来在移动。所以解决这个问题的方法基本上就是先把新窗体的位置设置为主窗体的位置。之后,我能够将位置设置为 MainForm 的中心。

TL;DR the new form's location was not relative to the parent form's location, but to a fixed position that I'm guessing is (0, 0)

TL;DR 新表单的位置不是相对于父表单的位置,而是相对于我猜测的固定位置 (0, 0)

I changed the MainForm's Startup Position to a fixed one for my own convenience. I also added an event to make sure that the new forms position always was the center of the MainForm.

为方便起见,我将 MainForm 的启动位置更改为固定位置。我还添加了一个事件以确保新表单位置始终位于 MainForm 的中心。

    private void Location_Changed(object sender, EventArgs e)
    {
        this.Location = this.Owner.Location;
        this.Left += this.Owner.ClientSize.Width / 2 - this.Width / 2;
        this.Top += this.Owner.ClientSize.Height / 2 - this.Height / 2;
    }

    private void ConnectingForm_Load(object sender, EventArgs e)
    {
        this.Owner.LocationChanged += new EventHandler(this.Location_Changed);
        this.Location = this.Owner.Location;
        this.Left += this.Owner.ClientSize.Width / 2 - this.Width / 2;
        this.Top += this.Owner.ClientSize.Height / 2 - this.Height / 2;
    }

Hopefully this will help others with the same problem!

希望这会帮助其他有同样问题的人!

回答by Reed Copsey

StartPosition should work fine with Form.Show. Try:

StartPosition 应该可以正常工作Form.Show。尝试:

ConnectingForm CF = new ConnectingForm();
CF.StartPosition = FormStartPosition.CenterParent;
CF.Show(this);

If you want to manually place the form, as you've shown, that can be done as well, but still requires setting the StartPositionproperty to Manual:

如果您想手动放置表单,如您所示,也可以这样做,但仍需要将StartPosition属性设置为Manual

ConnectingForm CF = new ConnectingForm();
CF.StartPosition = FormStartPosition.Manual;
CF.Location = new Point(this.ClientSize.Width / 2, this.ClientSize.Height / 2);
CF.Show();

On a side note, you shouldn't use a usingstatement with Form.Show. usingwill call Disposeon the form, which isn't desired, since the form's lifetime is longer than this block of code.

附带说明一下,您不应该使用using带有Form.Show. using将调用Dispose表单,这是不希望的,因为表单的生命周期比此代码块长。