调用Form.Show()时设置表单的位置 [英] Setting form's location when calling Form.Show()

查看:53
本文介绍了调用Form.Show()时设置表单的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 .Show()调用表单时设置表单的位置.问题是,因为我使用的是 .Show 而不是 .ShowDialog ,所以StartPosition值不起作用.我不能使用 .Showdialog ,因为我希望程序在显示表单时在后台运行.

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

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

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)

推荐答案

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

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;
    }

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

(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.

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

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

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.

    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!

这篇关于调用Form.Show()时设置表单的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆