Open Modal window in Center of Parent Form in MDI Application
I am working on Winform application and want to open modal form in center of parent form. In Winform application there is :
- MDI Form (open as startup form and act as container for all)
- on click of one of the Menu item of MDI Form - opens a MDI Child form
- 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 fo开发者_C百科rm 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
Try this:
TestModalForm.showdialog(this.MdiParent);
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
.
Your method seems overly 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);
I think you should do this
//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
In MDI forms center to parent
doesn't work as expected see the documentation MSDN ,it is always recommended to use center to screen
, but I use a work-around to this, after all it's about location positioning so I created an override method for center to parent
and used it on the child form load event
so that every time the child is loaded it is centered to parent:
private void CenterToParentOverride()
{
this.Location = new Point(
this.MdiParent.ClientSize.Width / 2 - this.Width / 2,
this.MdiParent.ClientSize.Height / 2 - this.Height / 2);
}
精彩评论