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.