Windows Forms: How to Add the Form in Panel from another Form in C#

This post shows you How to open a Form in a Panel in C# Windows Forms Application.

Adding a form to a panel from another form in C# involves a few steps

How to open a Form in a Panel using C#

Let's assume you have Form1 and Form2, and you want to add Form2 to a panel on Form1. Here's how you can do it

The panel is a Windows Forms control that lets you add other controls to it.

In addition to adding UserControl or Control to the Panel, sometimes you will need to create an application that allows you to open multiple Forms in a Panel from one Form. You can easily add Form in Panel C# in the following way.

Creating a new Windows Forms Application project, then open your form designer. Next, Drag and drop Button, Label, Combobox and Panel controls from your Visual Studio Toolbox to your winform.

Let's say Form1 is your main form, and Form2 is the form you want to add to a panel on Form1.

In the designer view of Form1, drag and drop a Panel control onto your form. Make sure it's large enough to accommodate Form2.

In Form1, where you want to add Form2 to the panel, create an instance of Form2, then set the parent of Form2 to be the panel on Form1. This will make Form2 a child control of the panel.

Position Form2 within the panel as desired, and then show Form2.

c# open multiple forms in panel

Next, Create a new Form with the name Form2, then you can design a simple UI as shown below.

simple winform in c#

Clicking the Combobox control in Form1, then select Edit Items...

string collection editor

Next, You need to add the above items into the combobox.

You can also create Form3, Form4...etc, then open your form similarly in the following way.

How to load a form inside another form in c#

Adding a click event handler to the ShowForm button allows you to open a new Form inside a Panel in C# as shown below.

private void btnShowForm_Click(object sender, EventArgs e)
{
    Form2 frm = new Form2() { TopLevel = false, TopMost = true };
    frm.FormBorderStyle = (FormBorderStyle)cboFormStyle.SelectedIndex;
    this.pContainer.Controls.Add(frm);
    frm.Show();
}

If you want to open new form in same window or load form inside panel other form in c# , i think this is the best solution.

In this example, pContainer is the panel on Form1 where you want to add Form2. The TopLevel property is set to false to indicate that Form2 is not a top-level window. Dock property is set to DockStyle.Fill to make Form2 fill the entire panel.

VIDEO TUTORIAL

Related