Click here to Skip to main content
15,943,077 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a form that has panel on it (named pnl_Control). pnl_Control is used to open other forms which works. Here is the code used.

frm_NotesView myForm = new frm_NotesView();
           myForm.TopLevel = false;
           myForm.AutoScroll = false;
           pnl_Control.Controls.Add(myForm);
           myForm.FormBorderStyle = FormBorderStyle.None;
           myForm.Show();


I want to change myForm.Show() to myForm.ShowDialog(). When I do a run time 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.' What am I doing wrong? The reason I want a ShowDialog is to update some data in the main form.

What I have tried:

frm_NotesView myForm = new frm_NotesView();
            myForm.TopLevel = false;
            myForm.AutoScroll = false;
            pnl_Control.Controls.Add(myForm);
            myForm.FormBorderStyle = FormBorderStyle.None;
            myForm.ShowDialog();
Posted
Updated 1-Aug-22 4:40am
Comments
Richard MacCutchan 1-Aug-22 10:22am    
"What am I doing wrong?"
Exactly what it says in the error messages. If you wish to use ShowDialog on a Form, then it must be a freestanding top level form.
Dave Kreskowiak 1-Aug-22 11:08am    
What are you doing wrong? Well, the error is telling you. You're trying to use ShowDialog to show a non-toplevel form. You can't do that.

1 solution

Read the error message, it's pretty explicit.
Modal forms must be top level: they cannot be embedding in controls within a form. If you think about what a modal form is it's pretty obvious why: the "contained" form would have to get all input at the expense of the container form - which is what filters messages to the contained form!

If you want to update data in the main form, that doesn't require a modal dialog, just an event handler in the "Parent" to access the "Child" data: Transferring information between two forms, Part 2: Child to Parent[^]

To be honest, I wouldn't use a form in the container anyway: I'd create a UserControl instead and embed that in a stand-alone form if I needed it displayed on it's own.
 
Share this answer
 
v2
Comments
Maciej Los 1-Aug-22 12:09pm    
5ed!

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900