Estoy trabajando en la aplicación Winform y quiero abrir el formulario modal en el centro del formulario principal. En aplicación Winform existe:Abrir la ventana modal en el Centro del formulario principal en la aplicación MDI
- formulario MDI (abierta como formulario de inicio y actuar como contenedor para todos)
- el clic de uno de los elementos de menú del formulario MDI - se abre un formulario secundario MDI
- en haga clic en uno de los botones en MDI Niño abierto en el paso 2 - abre un formulario modal - que tenemos que abrir en el centro de MDI Formulario secundario (abierto en el paso 2)
Para abrir el formulario modal en el centro 1 La solución obvia que había hecho es
TestModalForm obj = new TestModalForm()
obj.StartPosition = FormStartPosition.CenterParent;
obj.showdialog(this);
pero la solución anterior no funcionó ya que la forma modal siempre considera que MDI Form es su principal. entrenamiento así que por segunda solución: en la que escribí método en el formulario de carga de la ventana modal para colocarlo en el centro de la siguiente manera:
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;
}
encima solución funciona perfecto cuando formulario MDI se abre en forma maximizada. Pero cuando comprobamos cambiando el tamaño del formulario MDI (es decir, no en forma maximizada) o moviendo Formulario MDI a otra pantalla - en caso de pantallas múltiples, la solución anterior no funciona y no abre el formulario modal en el centro de MDI Child form
También miró this Question pero no es útil para mi problema.
Cualquier persona puede tener alguna sugerencia o solución para resolver el problema.
Gracias
if (this.MdiParent! = Null) ... –
Hola Davide, TestModalForm.showdialog (this.MdiParent); no funciona. También this.MdiParent da nulo por lo que se muestra en el centro de MDI, que no es necesario. Necesito abrir la tercera ventana modal en el centro de 2nd MDI Child. Su 3er punto en el comienzo de la pregunta. – Shah