C# 如何使用代码设置 Windows 窗体窗体的 StartPosition?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/225741/
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-03 18:55:08  来源:igfitidea点击:

How do you set the StartPosition of a Windows Forms form using code?

c#.netwinforms

提问by mattruma

Is there a way to set the StartPosition of a Windows Forms form using code? It seems whatever I try results in the StartPostion being the default.

有没有办法使用代码设置 Windows 窗体窗体的 StartPosition?似乎无论我尝试什么都会导致 StartPostion 成为默认值。

Here is what I am doing in the form to display:

这是我在表单中显示的内容:

    public DealsForm()
    {
        InitializeComponent();
        this.StartPosition = FormStartPosition.CenterParent;
    }

Here is what I am doing to display the form:

这是我为显示表单所做的工作:

    private void nvShowDeals_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
        DealsForm frm = new DealsForm();

        frm.DataSource = this.Deals;

        frm.Show(this);
    }

I have tried putting the following in each of the above methods, to no avail:

我尝试将以下内容放入上述每种方法中,但无济于事:

this.StartPosition = FormStartPosition.CenterParent;

If I set it via the Property Editor ... it works perfectly, but I would reallylike to do it via code.

如果我通过属性编辑器设置它......它工作得很好,但我真的很想通过代码来做。

Should be a no-brainer ... but for the life of me I can't seem to figure it out ... maybe I need more caffeine.

应该很简单……但对于我的生活,我似乎无法弄清楚……也许我需要更多的咖啡因。

Update:

更新:

If I do a ShowDialog()and pass the parent it works ... but I really don't want to show it as a Dialog.

如果我执行 aShowDialog()并通过父项,它会起作用……但我真的不想将其显示为对话框。

采纳答案by PersistenceOfVision

If I do a ShowDialog() and pass the parent it works ... but I really don't want to show it as a Dialog.

如果我执行 ShowDialog() 并传递父项,它会起作用……但我真的不想将其显示为对话框。

That is correct since ShowDialog would set frm.Parent == nvShowDeals.Parent
Since you are using .Show() then frm.Parent == null thus FormStartPosition.CenterParent is ignored.

这是正确的,因为 ShowDialog 会设置 frm.Parent == nvShowDeals.Parent
因为您使用的是 .Show() 然后 frm.Parent == null 因此 FormStartPosition.CenterParent 被忽略。

So to accomplish this function I would make the following changes:

因此,要完成此功能,我将进行以下更改:

public DealsForm()
{
    InitializeComponent();
    //this.StartPosition = FormStartPosition.CenterParent;
}

//DealsForm_Load Event
private void DealsForm_Load(object sender, EventArgs e)
{
    this.Location = this.Owner.Location;  //NEW CODE
}

And Here I would make the following changes:

在这里,我将进行以下更改:

private void nvShowDeals_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
    DealsForm frm = new DealsForm();

    frm.DataSource = this.Deals;
    frm.StartPosition = FormStartPosition.Manual; //NEW CODE
    frm.Show(this);
}

回答by Vordreller

My first reaction is: experiment a bit with VS2008. It should be in the general properties screen.

我的第一反应是:用 VS2008 做一些实验。它应该在常规属性屏幕中。

If you don't have Visual Studio, then it gets a little harder.

如果您没有 Visual Studio,那么它会变得有点困难。

A good site to check might be this one: csharp-online.net

一个值得检查的好网站可能是这个:csharp-online.net

Sorry that I can't be more helpfull

对不起,我不能提供更多帮助

回答by splattne

Did you try to set the property in the calling method?

您是否尝试在调用方法中设置属性?

private void nvShowDeals_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
    DealsForm frm = new DealsForm();

    frm.DataSource = this.Deals;

    // Insert this
    frm.StartPosition = FormStartPosition.CenterParent;

    frm.Show(this);
}

回答by Patrick Desjardins

public DealsForm()
{
    InitializeComponent();
    this.StartPosition = FormStartPosition.CenterParent;       
}

Try to put it before InitializeComponent(). It might be already too late after InitializeComponent (the form might be already launch and the StatPosition is set too late).

尝试将其放在 InitializeComponent() 之前。在 InitializeComponent 之后可能已经太晚了(表单可能已经启动并且 StatPosition 设置得太晚)。

Update

更新

I just wrote :

我刚刚写道:

public Form1()
{
    InitializeComponent();
    this.StartPosition = FormStartPosition.CenterScreen;
}

And:

和:

private void button1_Click(object sender, EventArgs e)
{
    Form1 f = new Form1();
    f.Show();
}

In a VS project (brand new) and when I click in my form2 a button it open the form in the middle of the screen. You can do the same with Parent...

在 VS 项目(全新)中,当我在 form2 中单击一个按钮时,它会在屏幕中间打开表单。您可以对 Parent 做同样的事情...

回答by Jon Grant

I'd suggest checking your DealsForm.Designer.cs and removing the line that sets the StartPosition in there, then doing it as you are.

我建议检查您的 DealsForm.Designer.cs 并删除在那里设置 StartPosition 的行,然后按原样执行。

Alternatively, perhaps try setting it in the Load or Shown events of the form instead.

或者,可以尝试在表单的 Load 或 Shown 事件中设置它。

回答by splattne

Maybe you are not alone. Maybe you are not insane. Read this (Microsoft Connect Customer Feedback):

也许你并不孤单。也许你不是疯子。阅读此内容(Microsoft Connect 客户反馈):

Windows Form StartPosition property only works for .ShowDialog method and not for .Show method

Windows 窗体 StartPosition 属性仅适用于 .ShowDialog 方法而不适用于 .Show 方法

Customer: "Windows Form StartPosition only works for .ShowDialog method and not for .Show method. Note: I have also attached simple code and images of the results."

客户:“Windows 窗体 StartPosition 仅适用于 .ShowDialog 方法,不适用于 .Show 方法。注意:我还附上了简单的代码和结果图像。”

MS: "Unfortunately, we will not be able to fix this particular issue in a future release, as a fix here would be a breaking change to the behavior of WinForms 1, 1.1 and 2"

MS:“不幸的是,我们将无法在未来的版本中修复这个特定问题,因为这里的修复将是对 WinForms 1、1.1 和 2 行为的重大更改”

回答by Justin Pihony

To center on parent for the .Show call, this is what I had to do:

为了在 .Show 调用中以 parent 为中心,这就是我必须做的:

childForm.Location = new Point(
    (parentForm.Location.X + parentForm.Width / 2) - (childForm.Width / 2), 
    (parentForm.Location.Y + parentForm.Height / 2) - (childForm.Height / 2));
childForm.StartPosition = FormStartPosition.Manual;

回答by Si Fitz

You can do this by calling this.CenterToParent() in the Form_Load event (when the parent is actually known). Don't call this in the Constructor because the parent it set when Show(form) is called.

您可以通过在 Form_Load 事件中调用 this.CenterToParent() 来做到这一点(当父级实际已知时)。不要在构造函数中调用它,因为它在调用 Show(form) 时设置了父对象。

private void myForm_Load(object sender, EventArgs e)
{
    CenterToParent();
}

I know this thread is old but it can be answered pretty easily so hopefully help others who come across it find the easy solution.

我知道这个帖子很旧,但可以很容易地回答,所以希望能帮助遇到它的其他人找到简单的解决方案。