How can i make an MDI form inactive when child form is active
I placed an MDI form in my 开发者_如何学Goapplication . If i select an option from file menu as New i will have a child form loaded.
My code is as follows to show the child form
private void ShowNewForm(object sender, EventArgs e)
{
foreach (Form frm in Application.OpenForms)
{
if (frm.Text == "Main")
{
IsOpen = true;
frm.Focus();
break;
}
}
if (IsOpen == false)
{
Form childForm = new FrmMain();
childForm.MdiParent = this;
childForm.Show();
}
}
Now what i need is when the child form is in active state i would like to have my MDI inactive until and unless the user closes the child form.
Generally for forms we will write
frm.showDialog()
So how to resolve this
give like this
if (IsOpen == false)
{
Form childForm = new FrmMain();
childForm.TopLevel=true;
childForm.ShowInTaskbar=false;
childForm.ShowDialog();
}
This is fundamental about MDI, a child form can not be made modal. You have to use ShowDialog() and make sure you don't set the MdiParent property. Such a dialog is not constrained by the boundaries of the MDI parent, you can use the StartPosition property to get it centered. Like this:
using (var dlg = new Form2()) {
dlg.StartPosition = FormStartPosition.CenterParent;
if (dlg.ShowDialog(this) == DialogResult.OK) {
// Use dialog properties
//...
}
}
Of course, you don't have to check anymore whether the form already exists, it is modal.
精彩评论