Home > Enterprise >  How to close Parent form without closing child form in Windows form in c#
How to close Parent form without closing child form in Windows form in c#

Time:03-21

I am trying to make one windows application which contains two forms. In form1(Parent) i created one button for open second form. On that button click event i want to close form1(parent) form and open form2(child) without closing form2, but when i am press that button both forms are closed so how can I do it?

CodePudding user response:

Rather than using .Close() consider using .Hide() on your parent form, this shouldn't dispose of it but rather have it hidden.

Note that exiting out of your Child form means your application won't exit. To circumvent this you should consider subscribing to the OnFormClosed event to handle the form closure.

CodePudding user response:

If you want to close form1(parent) form and open form2(child) form without closing form2 by click the button, you can refer to the following code:

private void button1_Click(object sender, EventArgs e)
    {
        Form2 form2 = new Form2();
        this.Hide();
        form2.Show();
    }

Here is the test result enter image description here

CodePudding user response:

If you look in "Program.cs" there is a form to start with.
When this form is closed, the application is terminated.

enter image description here



It should be used as a way to hide the startup form.
Even if the child form is closed with the start form hidden, the program does not end.
As @Ae774 said, you need to handle it separately.
  • Related