Open Modal window in Center of Parent Form in MDI Application

13,295

Solution 1

Try this:

TestModalForm.showdialog(this.MdiParent);

Solution 2

To show form using ShowDialog in center of its parent when the parent is MDI Child, you can use following code

The code is supposed to run from a button in a MDI Child form and show another form as modal dialog at center of the MDI Child form:

var dialog = new Form(); //The form which you want to show as dialog
//this.Parent point to the MdiClient control which is the container of this
var p = this.Parent.PointToScreen(this.Location); 
p.Offset((this.Width - dialog.Width)/2 , (this.Height - dialog.Height)/2);
dialog.StartPosition = FormStartPosition.Manual;
dialog.DesktopLocation = p;
dialog.ShowDialog();

In above code, since the current form is a MDI Child, then this.Parent point to the MdiClient control which is the container of MDI Child forms, so we can use its PointToScreen method passing location of MDI child (this) to get the screen location of MDI child. Then if you offset that location using the half of difference between MDI children and dialog width and height and set the StartPosition of dialog to FormStartPosition.Manual and show it using ShowDialog.

Share:
13,295
Shah
Author by

Shah

.Net Professional

Updated on June 25, 2022

Comments

  • Shah
    Shah almost 2 years

    I am working on Winform application and want to open modal form in center of parent form. In Winform application there is :

    1. MDI Form (open as startup form and act as container for all)
    2. on click of one of the Menu item of MDI Form - opens a MDI Child form
    3. on click of one of the button on MDI Child opened in step 2 - opens a modal form - which we need to open in center of MDI Child form (opened in step 2)

    So for opening modal form in center 1st obvious solution i had done is

    TestModalForm obj = new TestModalForm()
    obj.StartPosition = FormStartPosition.CenterParent;
    obj.showdialog(this);
    

    but above solution didn't work as modal form always considers MDI Form as its Parent. So I workout for 2nd solution: in that i wrote method in Form Load of Modal window to position it in center as below:

            private void MakeWinInCenter()
            {
                if (this.Owner != null)
                {
                    Form objParent = null;
                    int TopbarHeight = 0;
                    if (this.Owner.IsMdiContainer && this.Owner.ActiveMdiChild != null)
                    {
                        objParent = this.Owner.ActiveMdiChild;
                        TopbarHeight = GetTopbarHeight(this.Owner);
                    }
                    else
                        objParent = this.Owner;
    
                    Point p = new Point((objParent.Width - this.Width) / 2, (objParent.Height - this.Height) / 2);
                    p.X += objParent.Location.X;
                    p.Y += TopbarHeight + objParent.Location.Y;
                    this.Location = p;
                }
                else
                {
                  //If owner is Null then, we have reference of MDIForm in Startup Class - use that ref and opens win in center of MDI
                    if (Startup.MDIObj != null)
                    {
                        this.Left = Convert.ToInt32((Startup.MDIObj.Width - this.Width) / 2);
                        this.Top = Convert.ToInt32((Startup.MDIObj.Height - this.Height) / 2);
                    }
                }
    
            }
    
            private int GetTopbarHeight(Form MDIForm)
            {
                int TopbarHeight = 0;
                MdiClient objMDIClient = null;
                foreach (Control ctl in MDIForm.Controls)
                {
                    if (ctl is MdiClient)
                    {
                        objMDIClient = ctl as MdiClient;
                        break;
                    }
                }
                if (objMDIClient != null)
                {
                    TopbarHeight = MDIForm.Height - objMDIClient.Size.Height;
                }
                return TopbarHeight;
            }
    

    Above solution works perfect when MDI form is opened in Maximized form. But when we checked by resizing the MDI form (i.e. not in maximized form) or moving MDI Form to other screen - in case of multiple screens, above solution is not working and doesn't opens the modal form in center of MDI Child form

    Also looked at this Question but its not helpful to my problem.

    Can anyone have any suggestions or solution to solve the problem.

    Thanks

  • Shah
    Shah almost 13 years
    Hello Davide, TestModalForm.showdialog(this.MdiParent); its not working. Also this.MdiParent gives null so it displays in center of MDI - which is not required. I need to open 3rd modal window in center of 2nd MDI Child. Its 3rd point in start of question.
  • Reza Aghaei
    Reza Aghaei almost 8 years
    The OP wants to show the dialog in center of MDI Child not in center of MDI Parent Form.
  • Reza Aghaei
    Reza Aghaei almost 8 years
    It makes the dialog show in center of MDI Parent, while the OP need to show in in center of MDI Child form.
  • Reza Aghaei
    Reza Aghaei almost 8 years
    I know the question is old, but currently all other answers are wrong. A question with 10k views needs a correct answer.