I am working on Winform application and want to open modal form in center of parent form. In Winform application there is :
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
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With