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

后端 未结 19 1044
不思量自难忘°
不思量自难忘° 2020-11-28 08:21

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

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

I need to display the chi

相关标签:
19条回答
  • 2020-11-28 09:06

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

    Try this:

    loginForm = new SubLogin();
    loginForm.Show(this);
    loginForm.CenterToParent()
    
    0 讨论(0)
  • 2020-11-28 09:07

    You can set the StartPosition in the constructor of the child form so that all new instances of the form get centered to it's parent:

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

    Of course, you could also set the StartPosition property in the Designer properties for your child form. When you want to display the child form as a modal dialog, just set the window owner in the parameter for the ShowDialog method:

    private void buttonShowMyForm_Click(object sender, EventArgs e)
    {
        MyForm form = new MyForm();
        form.ShowDialog(this);
    }
    
    0 讨论(0)
  • 2020-11-28 09:08
    childform = new Child();
    childform.Show(this);
    

    In event childform load

    this.CenterToParent();
    
    0 讨论(0)
  • 2020-11-28 09:08

    If you have to center your childForm, from childForm then the code will be something like this. This code is in the childForm.cs

    this.Show(parent as Form);    // I received the parent object as Object type
    this.CenterToParent();
    
    0 讨论(0)
  • 2020-11-28 09:10

    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.

    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.

    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;
        }
    }
    
    0 讨论(0)
  • 2020-11-28 09:11

    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.

    0 讨论(0)
提交回复
热议问题