Open the Modal window in the Center of the parent form in the MDI application

I am working on a Winform application and want to open a modal form in the center of the parent form. The Winform application has:

  • MDI form (open as a launch form and acts as a container for everyone)
  • when you click on one of the menu items of the MDI form, the MDI child form opens
  • when you click one of the buttons on the MDI Child, open in step 2, the modal form opens, which we must open in the center of the MDI Child form (open in step 2).

So, to open the modal form in the center of the 1st obvious decision I made was

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

but the solution above does not work, since the modal form always considers the MDI Form as its parent. Therefore, I train for the second solution: in that I wrote a method in the form of Loading a modal window to place it in the center, as shown 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); pX += objParent.Location.X; pY += 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; } 

The above solution works perfectly when the MDI form opens in a maximized form. But when we checked by changing the MDI form (i.e. Not in a maximized form) or moving the MDI form to another screen - in the case of several screens above, the solution does not work and does not open the modal form in the center of the MDI Child form

Also looked at this question , but that did not help my problem.

Anyone have suggestions or solutions to solve the problem.

thanks

+6
source share
5 answers

Try the following:

 TestModalForm.showdialog(this.MdiParent); 
+1
source

Your method seems too complicated. Why not just do it this way?

 // All code goes in your MDI Child form // Create the modal form TestModalForm obj = new TestModalForm(); // Find center point of MDI Parent form Point centerPoint = new Point(this.MdiParent.Top + this.MdiParent.Height / 2, this.MdiParent.Left + this.MdiParent.Width / 2); // Set the location and show the modal form obj.StartPosition = FormStartPosition.Manual obj.Location = centerPoint; obj.ShowDialog(this); 
0
source

I think you should do it

 //for modal form set its strat position to centerparent like this **this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;** // this will open the modalform in center of parent form 
0
source

In MDI forms, center to parent does not work as expected, in the MSDN documentation, it is always recommended to use center to screen , but I use a workaround for this, after all, about location location, so I created an override method for center to parent and used its in child form load event , so that every time a child loads, it is centered on the parent

 private void CenterToParentOverride() { this.Location = new Point( this.MdiParent.ClientSize.Width / 2 - this.Width / 2, this.MdiParent.ClientSize.Height / 2 - this.Height / 2); } 
0
source

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

The code should start from a button in the form of an MDI child object and show another form as a modal dialog in the 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 the above code, since the current form is an MDI child, then this.Parent points to the MdiClient element, which is the MDI Child form container, so we can use the PointToScreen method, passing the location of the MDI parent (this) to get the location of the MDI screen of the child . Then, if you compensate for this location using half the difference between the MDI children and the width and height of the dialog, set the StartPosition dialog to FormStartPosition.Manual and show it using ShowDialog .

0
source

Source: https://habr.com/ru/post/890479/


All Articles