tags:

views:

81

answers:

2

I placed an MDI form in my application . 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

+1  A: 

give like this

if (IsOpen == false)
        {

    Form childForm = new FrmMain();
         childForm.TopLevel=true;
         childForm.ShowInTaskbar=false;
         childForm.ShowDialog();
        }
Vyas
But the form will not be inside MDI if we move the from right
Dorababu
by giving TopLevel=true,MDI inactive until and unless the user closes the child form.But its not having MDI parent
Vyas
But inorder to make the form inside the MDI can't we do this.
Dorababu
set the childForm's ShowInTaskbar property to false.
Vyas
Even then if i move my child form it is moving ouside the MDI.
Dorababu
I think MDIChild forms cannot be shown as Modal. You will have to create a non-mdi child form which can be shown as modal.
Vyas
Ok any way thanks for your help. :)
Dorababu
+1  A: 

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.

Hans Passant