Click here to Skip to main content
14,357,784 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have a mdi parent form with the tool bar. And that form contains panel that showing the company details (name, addresses,logo). I need to open a child form on this by clicking a menu item. When child form loads that panel in the main form should not visible. After closing the child form it should be displayed. On menu item clicked my code is like this,

panel1.Visible = false;  
form1 frm = new form1();
            
frm.MdiParent = this;
            
frm.ShowDialog(this);
panel1.Visible = true;


But I'm getting error "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."
Posted
Rate this:
Please Sign up or sign in to vote.

Solution 1

This is what it is: remove the line "frm.MdiParent = this". There is no situation when you need a form to be a MDI child and a modal dialog at the same time.

Speaking of MDI: who needs it? Don't torturing yourself and scare off your users.
See:
http://en.wikipedia.org/wiki/Multiple_document_interface#Disadvantages[^],
Question on using MDI windows in WPF[^],
MDIContainer giving error[^].

—SA
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 2

Use this code on item clicked.
panel1.Visible = false;
form1 frm = new form1();
frm.ShowDialog();

Use the below code in MDIParent2_Activated event.
private void MDIParent_Activated(object sender, EventArgs e)
{
   panel1.Visible = true;
}
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

You can create a custom class deriving from System.Windows.Forms.Form. Pass the parent form's ref. to the constructor while creating the child form. In the constructor hide the parent form using reference. On form closing event show the parent form again using the reference.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100