Adding child forms to a form

N

Nico Vrouwe

Hi,

I'm fairly new to Windows Forms, and I'm wondering how to add a child form
to my form? I don't want my main form to be an MDI form, I just want to put
a child form in it. Like putting in a window with WS_CHILD style.
I want to be able to edit all child forms like normal forms using the forms
designer. And show a given child form in the main form when needed.

When I try this:
Form childForm = new SomeForm();
childForm.Parent = this;
I get an error "Cannot add a top level control to a control"

Perhaps I overlooked something, but I can't find a solution anywhere.
Anyone?

Thanks in advance,

Nico Vrouwe
 
N

Nico Vrouwe

Hi,

Actually all forms are supposed to have different contents.
I solved it for now by creating a base Form class containing an empty Panel.
Then I have a bunch of Forms derived from that one, and put some controls in
the panel. Then at runtime I set the parent of each panel to the main form
and move it to the right position.
Quite ugly but it works ;)

But thanks anyway,

/Nico

PocketNerd said:
If you own the two forms in question, you could create a single user
control with all of the details on it that you want to see in both forms,
and then simply add that user control to both forms.
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Nico,

The form has a property TopLevel, which is not exposed at design time. What
you might wanna do is to set that proerty to false for the child form. Be
adviced that you can not use the designer to add the form, however you can
edit the second form in the designer as any other form.

The code to add the second form should look something like

Form childForm = new SomeForm();
childForm.TopLevel = fase; //This changes the style of the form's window to
WS_CHILD
this.SuspendLayout()
this.Controls.Add(childForm)
childForm.Dock = DockSyle.Top; //or any other docking style
childForm.Show(); //you don't do that for the normal controls, but the forms
have to be shown explicitly.
this.ResumeLayout()
 
N

Nico Vrouwe

Hi,

Thanks a million! This is just what I was looking for :)

/Nico

Stoitcho Goutsev (100) said:
Hi Nico,

The form has a property TopLevel, which is not exposed at design time. What
you might wanna do is to set that proerty to false for the child form. Be
adviced that you can not use the designer to add the form, however you can
edit the second form in the designer as any other form.

The code to add the second form should look something like

Form childForm = new SomeForm();
childForm.TopLevel = fase; //This changes the style of the form's window to
WS_CHILD
this.SuspendLayout()
this.Controls.Add(childForm)
childForm.Dock = DockSyle.Top; //or any other docking style
childForm.Show(); //you don't do that for the normal controls, but the forms
have to be shown explicitly.
this.ResumeLayout()

--
HTH
B\rgds
100 [C# MVP]

Nico Vrouwe said:
Hi,

I'm fairly new to Windows Forms, and I'm wondering how to add a child form
to my form? I don't want my main form to be an MDI form, I just want to put
a child form in it. Like putting in a window with WS_CHILD style.
I want to be able to edit all child forms like normal forms using the forms
designer. And show a given child form in the main form when needed.

When I try this:
Form childForm = new SomeForm();
childForm.Parent = this;
I get an error "Cannot add a top level control to a control"

Perhaps I overlooked something, but I can't find a solution anywhere.
Anyone?

Thanks in advance,

Nico Vrouwe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top