Opening the new form and closing the previous

Shaifali jain 420 Reputation points

Hi ,

In my project i am stuck at a point and can not figure out how to open a new form inside a panel of already opened form . So there is a form named father and it has a panel . i am using this panel to open all subsequent forms (children) . there are two problems i am facing

  1. before the new form is opened , i have to check whether a new form is already opened or not inside the fathers panel . if yes a form is already opened inside panel , it should close first and then only the new child can open .
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,730 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
9,450 questions
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 13,341 Reputation points Microsoft Vendor

    Hi @Shaifali jain , Welcome to Microsoft Q&A.

    Your premise is to close the previous form. You only need to use a simple method to determine whether Panel.Controls.Count is 0, and then use the built-in clear().

    private void button1_Click(object sender, EventArgs e)
        if (FatherPanel.Controls.Count != 0)
        // Add new subform after clearing
        ChildForm newChildForm = new ChildForm();
        newChildForm.TopLevel = false;

    Best Regards,


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

1 additional answer

Sort by: Most helpful
  1. KOZ6.0 4,015 Reputation points

    I made sample

            private void button1_Click(object sender, EventArgs e) {
            private void RecreateForm<T>() where T : Form, new() {
                foreach (T oldForm in 
                            FatherPanel.Controls.OfType<T>().ToArray()) {
                    using (oldForm) {
                var newForm = new T {
                    TopLevel = false,
                    Visible = true