How To: Add Form as child of Form

As anyone who has taken a look at Form.Designer in Visual Studio knows, Form creation and Controls are all created at runtime with code.  With this knowledge, it becomes possible to create your own forms dynamically at runtime as well, or even just modify the Form.

Difficulty: Beginner

Adding default blank Form

Form child = new Form();
child.Text = "New Child Form";
child.TopLevel = false;
this.Controls.Add(child);
child.Parent = this;
child.Show();

Adding default Form1 to self

Form1 child = new Form1();
child.Text = "New Child Form";
child.TopLevel = false;
this.Controls.Add(child);
child.Parent = this;
child.Show();

The important section of this code is actually where Toplevel is set.  If you do not set Toplevel to false, your code will throw an Exception.

Images

Child Form Tutorial
Children Forms

Leave a Comment