2
votes

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

2
I'm not clear as to why do you need child forms to be MDI, when you're using them as Dialog forms? Why not childForm.ShowDialog()?veljkoz
If i use childform.showdialog() i am getting an error as Form that is not a top-level form cannot be displayed as a modal dialog box. Remove the form from any parent form before calling showDialog.Developer
I'm not sure it is possible or not, but I think your ui design is interesting. I didn't see this approach in any other application.anilca

2 Answers

3
votes

give like this

if (IsOpen == false)
        {

    Form childForm = new FrmMain();
         childForm.TopLevel=true;
         childForm.ShowInTaskbar=false;
         childForm.ShowDialog();
        }
2
votes

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.