1

So I want to show a form and close the other form so I did this in form1:

    private void newTaskToolStripMenuItem_Click(object sender, EventArgs e)
{
    Form2 x = new Form2();
    x.Show();
    this.Hide();
    //didn't use this.close() cause then the whole program will close instead of 
    //just the current form
}

Then I wanted the main form to open again after the 2nd form is closed so I did this in form2:

private void Form2_FormClosed(object sender, FormClosedEventArgs e)
    {
        Form1 x = new Form1();
        x.Show();
    }

Now the problem is when I'm back in the main form after closing the 2nd form. if I close the main form it doesn't fully close and still remains open in the background (I found it in task manager). I think its because the "show" method just opens another form instead of making the form appear so the main form which got hidden is still there running in the background. what should I do to make the form get closed when I exit it? I tried putting this.close(); in the form closed and form closing event but both resulted in a crash.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
  • 2
    The `Form2_FormClosed` event is creating a “new” `Form1` and a `From1` already exist! So you may be showing/closing `Form1` in the code, but it is not the `Form1` you are wanting to Show or Close. In the `newTaskToolStripMenuItem_Click` … have you tried… `x.ShowDialog()` THEN, you can close `Form1`. Something like… `x.ShowDialog();` then `this.Close();` … since the form is showing as a “dialog” the execution will “wait” until `Form2` closes before calling `this.Close();`. – JohnG Nov 14 '21 at 14:29
  • In addition… you should take note of one particular line of code… `this.Hide();` … basically… when execution passes this line of code… then… technically… you have LOST any reference to `form1`… that is why you are having to “create” a new `Form1` in `form2`. You can certainly pass `form1` to `form2`, however, this is unnecessary if you simply want to close `form1` when `form2` closes. Just use `form2.ShowDialog()` and use `this.Close()` as you originally intended. – JohnG Nov 14 '21 at 14:37
  • 1
    https://stackoverflow.com/questions/10769193/how-do-i-prevent-the-app-from-terminating-when-i-close-the-startup-form – Hans Passant Nov 14 '21 at 14:42

2 Answers2

2

When you write:

 Form1 x = new Form1();

you are creating a new Form1 object, so x refers to the new one and not to the original one. Instead, you could use this:

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

When ShowDialog() is called, the code following it is not executed until after the dialog box is closed, so this.Show() will execute only after Form2 is closed.

Michael Haddad
  • 4,085
  • 7
  • 42
  • 82
  • thx. also can you please answer some of my questions about the code? what does the "using" keyword do? "(var form2 = new Form2())"-----> "var" is to let the computer set the data type right? so i can just say "Form2 form2 = new Form2()" and it will still work right? – samyarkhafan Nov 14 '21 at 16:17
  • When you are using `ShowDialog()`, the form actually exists in memory even after you close it, so we need to dispose it. The `using` keyword saves us the need to manually do it, and does this automatically when the form closes. I suggest you read about the `IDisposable` interface and read [this](https://stackoverflow.com/a/39501121/1925272) answer. – Michael Haddad Nov 14 '21 at 16:28
  • `var x = new Form()` is the same as `Form x = new Form()`, it's just a shorthand that saves you the need to write the same thing twice. – Michael Haddad Nov 14 '21 at 16:28
0

Another option is to simply subscribe to the FormClosed() event of Form2 when you create it, then un-hide your instance of Form1 from there:

// ... all in Form1 ...

private void newTaskToolStripMenuItem_Click(object sender, EventArgs e)
{
    this.Hide();
    Form2 x = new Form2();
    x.FormClosed += X_FormClosed;
    x.Show();
}

private void X_FormClosed(object sender, FormClosedEventArgs e)
{
    this.Show();
}

So then when you close Form2 your instance of Form1 will automatically re-appear.

Idle_Mind
  • 38,363
  • 3
  • 29
  • 40