0

I want to open new form f2 and close current form f1 but when i tried this.close both forms close and this.hide just hide the form.

        Form2 f2 = new Form2();
        f2.ShowDialog();
        this.Close(); //this.Hide();
  • 3
    Possible duplicate of [c# open a new form then close the current form?](https://stackoverflow.com/questions/5548746/c-sharp-open-a-new-form-then-close-the-current-form) – SᴇM Jun 24 '19 at 10:00
  • How about you this.Close() first, then f2.Show()? – iSpain17 Jun 24 '19 at 10:01
  • 1
    *Open* the new form, *close* the current form. – bolkay Jun 24 '19 at 10:02
  • [How do I prevent the app from terminating when I close the startup form?](https://stackoverflow.com/a/10769349/7444103) – Jimi Jun 24 '19 at 10:29
  • If you want to hide the first form and show the second form and when you close the second form both forms will close then you are on the right track just add `this.Hide()`; at start of a code – Mangesh Auti Jun 24 '19 at 11:04
  • You can find the answer here https://stackoverflow.com/questions/12330104/closing-parent-without-closing-child. I had difficulty closing form2 after making this change, so I added Application.Exit() to the FormClosing event of form2. – Lucax Jun 24 '19 at 11:21

2 Answers2

0

Try Show method, Showdialog will act like f2 is the children of f1 and close both form.

   Form2 f2 = new Form2();
   f2.Show();
   this.Close();
  • The *problem* is that `this` is, apparently, the starting Form, so `this.Close();` will terminate the application. – Jimi Jun 24 '19 at 10:56
0

Close() on the main window normally terminates the program.

ShowDialog() shows the Form as modal dialog. The calling function will wait until the dialog is closed.

If you display a dialog, you should not try to close the calling window. If you need to display just another window, use Form.Show()

Daniel Schmid
  • 647
  • 6
  • 12