Opening the new form and closing the previous

Shaifali jain 420 Reputation points
2023-09-23T11:35:39.78+00:00

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,884 questions
C#
C#
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.
10,830 questions
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 44,321 Reputation points Microsoft Vendor
    2023-09-26T09:51:21.02+00:00

    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)
        {
            FatherPanel.Controls.Clear();
        }
    
        // Add new subform after clearing
        ChildForm newChildForm = new ChildForm();
        newChildForm.TopLevel = false;
        FatherPanel.Controls.Add(newChildForm);
        newChildForm.Show();
    }
    

    Best Regards,

    Jiale


    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 6,395 Reputation points
    2023-09-23T12:02:10.0833333+00:00

    I made sample

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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.