In your project, add 3 forms (your naming convention suggests you are using Form1 as the master with Form2 and Form3 as child forms). Add a panel to Form1 and a panel to Form2. Add a button to Form1; this button will be used to add an instance of Form2 into the panel on Form1. In Form2, add a button with a similar purpose; this time aiming to add an instance of Form3 into the panel in Form2. (Note, I have kept the name of the panels to the default of panel1 in both cases).
Now, add the following class to add the forms to the panels.
public static class PanelManager
{
public static void AddForm<T>(this Panel panel) where T : Form, new()
{
T form = new T
{
TopLevel = false,
TopMost = true
};
panel.Controls.Add(form);
form.Show();
}
}
Form1
In Form1, add a button click event handler and add the following code inside it.
panel1.AddForm<Form2>();
Form2In Form2, add a button click event handler, with the following code:
panel1.AddForm<Form3>();
Run the application and click the button in Form1 - this adds an instance of Form2 inside the panel. Click the button on Form2 to add Form3 into the panel in Form2. Now, click the close button in Form3 and this closes Form3 only.