0

I have three Forms (form1, form2, form3). When form1 loads it goes to form2 by using this code:

this.Hide();
Form2 frm = new Form2();
frm.ShowDialog();
this.Close();

form2 contains a button to show form3 and hide form2 by using this code in the button:

Form3 frm = new Form3();
frm.Show();
this.Hide();

the problem is when I click on this button the application exits.

I need to show form3 and hide form2 when the button is clicked.

Milo
  • 3,365
  • 9
  • 30
  • 44
kamal
  • 11
  • in your form 1 code, youve said on form2 going away, close form1, which shuts the app down.. most likely.. – BugFinder Jul 04 '19 at 12:11
  • remove this.Close(); – Mohammad Ghanem Jul 04 '19 at 12:12
  • but i write the close after showdialog to form2 mean it need to close form2 to excute the close of form1 – kamal Jul 04 '19 at 12:15
  • are you getting any exceptions? this.Close() is fine for form1 so that is not the problem. – man_luck Jul 04 '19 at 12:15
  • @kamal, however Id bet that form1 is the main form of your app. And when it closes, so will your application. – BugFinder Jul 04 '19 at 12:20
  • @BugFinder yes i know it main form but i make it use for first run and then the run begin from the form2 forthat i make form2 showdialog to be when it close the app close too – kamal Jul 04 '19 at 12:27
  • Possible duplicate of [How to open a new form from another form](https://stackoverflow.com/questions/3965043/how-to-open-a-new-form-from-another-form) – Owen Pauling Jul 04 '19 at 12:35
  • @kamal the this.close() will only run when the form2 exits "showdialog".. so its not actually running possibly when you thought. – BugFinder Jul 04 '19 at 12:39
  • Winforms will not allow you to Hide() a dialog, it will automatically close it. You need to do this differently, Close() the form right away and Show() the next one. To stop that from terminating your app, use [this code](https://stackoverflow.com/a/10769349/17034). – Hans Passant Jul 04 '19 at 19:04
  • @HansPassant That's right thank you very much – kamal Jul 04 '19 at 19:21

1 Answers1

0

When you call Hide() against Form2, execution returns to the ShowDialog() line.

Instead of ShowDialog(), use Show(). You can subscribe to the FormClosed() event to know when the form was closed.

Form1:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

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

    private void Frm_FormClosed(object sender, FormClosedEventArgs e)
    {
        this.Close();
    }
}

Form2:

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.Hide();
        Form3 frm = new Form3();
        frm.FormClosed += Frm_FormClosed;
        frm.Show();
    }

    private void Frm_FormClosed(object sender, FormClosedEventArgs e)
    {
        this.Close();
    }
}

Form3:

public partial class Form3 : Form
{
    public Form3()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.Close();
    }
}
Milo
  • 3,365
  • 9
  • 30
  • 44
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40