I am unable to open a child form as a modal form.

Here is my scenario

I have a MainForm which is a toplevel form.
Inside the MainForm I have a Toolbar Form which is a child.
From the Toolbar Form I call another child form.
From that child form I need to call myModalForm.

I have tried:

myModalForm myform = new myModalForm ();
myform.TopLevel = true;
myform.MdiParent = this.mdiparent;
myform.ShowDialog();

Result: '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.'

So I thought I would close the existing form first and then call the modal form

this.close();
myModalForm myform = new myModalForm ();
myform.TopLevel = true;
myform.ShowDialog();

Result: IT works...however, it is not inside the top level form.

So I thought I would open an instance of the MainForm and set that as the MDIParent

this.Close(); //Close Current Form
MainForm topform = new MainForm(); // Create new instance of MainForm
myModalForm myform = new myModalForm ();
topform.TopLevel = true;
myform.MdiParent = topform;
myform.ShowDialog();

Result: '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.'

Can anyone help me get a modal form within my toplevel MainForm?
I thought this should be simple...Am I missing something?

Thanks

Recommended Answers

All 3 Replies

Did you try

this.Modal = true;

in your mainform's designer? And then just open it with a .show();

setting this.modal = true; to the myModalForm produces an error:
Error 1 Property or indexer 'System.Windows.Forms.Form.Modal' cannot be assigned to -- it is read only.

I need myModalForm to show within my top level form (MainForm).

myModalForm is a form that is called from a child form, which is called from a child form which is contained in the MainForm. Therefore I cannot call the child form from the MainForm...

I have found a work around but it is not pretty.

What I have done is open the modal form from the MainForm. I have to call this method from my Child form. This works however, for every modal form in my application, I have to setup a new entry in my MainForm.

public void Open_Modal_Form(string formname)
        {
            switch (formname)
            {
                case "myModalForm":
                    myModalForm myform = new myModalForm();
                    myform.ShowDialog(this);
                    break;
            }
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.