在C#中在父窗体的中心显示子窗体

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

Show a child form in the centre of Parent form in C#

c#winforms

提问by

I create a new form and call from the parent form as follows:

我创建一个新表单并从父表单调用,如下所示:

loginForm = new SubLogin();   
loginForm.Show();

I need to display the child form at the centre of the parent. So,in the child form load I do the foll:`

我需要在父窗体的中心显示子窗体。因此,在子窗体加载中,我执行以下操作:`

Point p = new Point(this.ParentForm.Width / 2 - this.Width / 2, this.ParentForm.Height / 2 - this.Height / 2);
this.Location = p;

But this is throwing error as parent form is null. I tried setting the Parent property as well, but didn't help. Any inputs on this?

但这会引发错误,因为父表单为空。我也尝试设置 Parent 属性,但没有帮助。对此有任何意见吗?

采纳答案by Quintin Robinson

Try:

尝试:

loginForm.StartPosition = FormStartPosition.CenterParent;
loginForm.ShowDialog(this);

Of course the child for will now be a blocking form (dialog) of the parent window, if that isn't desired then just replace ShowDialogwith Show..

当然,子 for 现在将是父窗口的阻塞形式(对话框),如果不需要,则只需替换ShowDialogShow..

loginForm.Show(this);

You will still need to specify the StartPosition though.

不过,您仍然需要指定 StartPosition。

回答by BFree

On the SubLogin Form I would expose a SetLocation method so that you can set it from your parent form:

在 SubLogin 表单上,我将公开一个 SetLocation 方法,以便您可以从父表单设置它:

public class SubLogin : Form
{
   public void SetLocation(Point p)
   {
      this.Location = p;
   }
} 

Then, from your main form:

然后,从您的主要形式:

loginForm = new SubLogin();   
Point p = //do math to get point
loginForm.SetLocation(p);
loginForm.Show();

回答by Matthew Scharley

Assuming your code is running inside your parent form, then something like this is probably what you're looking for:

假设您的代码在您的父表单中运行,那么您可能正在寻找这样的东西:

loginForm = new SubLogin();
loginForm.StartPosition = FormStartPosition.CenterParent
loginForm.Show(this);

For the record, there's also a Form.CenterToParent()function, if you need to center it after creation for whatever reason too.

为了记录,还有一个Form.CenterToParent()功能,如果您也需要在创建后出于任何原因将其居中。

回答by jean

The parent probably isn't yet set when you are trying to access it.

当您尝试访问它时,父级可能尚未设置。

Try this:

尝试这个:

loginForm = new SubLogin();
loginForm.Show(this);
loginForm.CenterToParent()

回答by Stefan Steiger

You need this:

你需要这个:

Replace Me with this.parent, but you need to set parent before you show that form.

用 this.parent 替换 Me,但您需要在显示该表单之前设置 parent。

  Private Sub überToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles überToolStripMenuItem.Click

        'About.StartPosition = FormStartPosition.Manual ' !!!!!
        About.Location = New Point(Me.Location.X + Me.Width / 2 - About.Width / 2, Me.Location.Y + Me.Height / 2 - About.Height / 2)
        About.Show()
    End Sub

回答by Olivier Jacot-Descombes

There seems to be a confusion between "Parent" and "Owner". If you open a form as MDI-form, i.e. imbedded inside another form, then this surrounding form is the Parent. The form property StartPosition with the value FormStartPosition.CenterParent refers to this one. The parameter you may pass to the Show method is the Owner, not the Parent! This is why frm.StartPosition = FormStartPosition.CenterParent does not work as you may expect.

“父母”和“所有者”之间似乎存在混淆。如果您以 MDI 形式打开一个表单,即嵌入在另一个表单中,那么这个周围的表单就是父表单。具有值 FormStartPosition.CenterParent 的表单属性 StartPosition 指的是这个。您可以传递给 Show 方法的参数是 Owner,而不是 Parent!这就是为什么 frm.StartPosition = FormStartPosition.CenterParent 不像您预期​​的那样工作。

The following code placed in a form will center it with respect to its owner with some offset, if its StartPosition is set to Manual. The small offset opens the forms in a tiled manner. This is an advantage if the owner and the owned form have the same size or if you open several owned forms.

如果其 StartPosition 设置为 Manual,则放置在表单中的以下代码将其相对于其所有者居中并带有一些偏移。小偏移量以平铺方式打开表单。如果所有者和拥有的表单具有相同的大小,或者如果您打开多个拥有的表单,这将是一个优势。

protected override void OnShown(EventArgs e)
{
    base.OnShown(e);
    if (Owner != null && StartPosition == FormStartPosition.Manual) {
        int offset = Owner.OwnedForms.Length * 38;  // approx. 10mm
        Point p = new Point(Owner.Left + Owner.Width / 2 - Width / 2 + offset, Owner.Top + Owner.Height / 2 - Height / 2 + offset);
        this.Location = p;
    }
}

回答by timothy

When launching a form inside an MDIFormform you will need to use .CenterScreeninstead of .CenterParent.

当启动一个内部的表格MDIForm形式,您将需要使用.CenterScreen代替.CenterParent

FrmLogin f = new FrmLogin();
f.MdiParent = this;
f.StartPosition = FormStartPosition.CenterScreen;
f.Show();

回答by quixoteloco

Why not use this?

为什么不使用这个?

LoginForm.WindowStartupLocation = Windows.WindowStartupLocation.CenterOwner 

(vb.net)

(vb.net)

回答by Hessy SharpSabre

Make a Windows Form , then put option for it : CenterParent then use this Code :

制作一个 Windows 窗体,然后为其添加选项:CenterParent 然后使用此代码:

yourChildFormName x = new yourChildFormName();
x.ShowDialog();

回答by Lex van Buiten

If you want to calculate your own location, then first set StartPositionto FormStartPosition.Manual:

如果要计算自己的位置,则首先设置StartPositionFormStartPosition.Manual

Form Child = new Form();
Child.StartPosition = FormStartPosition.Manual;
Child.Location = new Point(Location.X + (Width - Child.Width) / 2, Location.Y + (Height - Child.Height) / 2);
Child.Show(this);

Where this is the main/parent form, just like Location.X.

这是主/父表单,就像 Location.X 一样。

Default value for StartPositionis FormStartPosition.CenterParentand therefore it changes the child's location after showing.

StartPositionis 的默认值FormStartPosition.CenterParent,因此它会在显示后更改孩子的位置。