14
votes

There is two Forms in my project : Form1 and Form2. There is a button in Form1, and what I want to do is closing Form1 and showing Form2 when that button clicked.

First, I tried

Form2 frm = new Form2();
frm.Show();
this.Close();

but as Form1 was closed, Form2 also got closed. Next, I tried

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

but there is a disadvantage that the application does not exit when the Form2 is closed.So, I had to put in additional sources inĀ form_FormClosing section of Form2.

Hmm.... I wonder whether this is the right way....So, what is the proper way of handling this problem?

4

4 Answers

35
votes

The auto-generated code in Program.cs was written to terminate the application when the startup window is closed. You'll need to tweak it so it only terminates when there are no more windows left. Like this:

    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        var main = new Form1();
        main.FormClosed += new FormClosedEventHandler(FormClosed);
        main.Show();
        Application.Run();
    }

    static void FormClosed(object sender, FormClosedEventArgs e) {
        ((Form)sender).FormClosed -= FormClosed;
        if (Application.OpenForms.Count == 0) Application.ExitThread();
        else Application.OpenForms[0].FormClosed += FormClosed;
    }
2
votes

By default, the first form controls the lifetime of a Windows Forms application. If you want several independent windows forms your application context should be a separate context from the forms.

public class MyContext : ApplicationContext
{
   private List<Form> forms;     

   private static MyContext context = new MyContext();

   private MyContext()
   {
      forms = new List<Form>();
      ShowForm1();
   }

   public static void ShowForm1()
   {
      Form form1 = new Form1();
      context.AddForm(form1);
      form1.Show();
   }

   private void AddForm(Form f)
   { 
      f.Closed += FormClosed;
      forms.Add(f);
   }

   private void FormClosed(object sender, EventArgs e)
   {
      Form f = sender as Form;
      if (form != null)
          forms.Remove(f);
      if (forms.Count == 0)
         Application.Exit();
   }          
}

To use the context, pass it to Application.Run (instead of the form). If you want to create another Form1, call MyContext.ShowForm1() etc.

public class Program
{
   public void Main()
   {
      Application.Run(new MyContext());
   }
}
-1
votes

You can take this way:

form2 f2=new form2()
this.Hide();
f2.Show();

Hope it was helpful.

-1
votes

Write that into your method which is executed while FormClosing event occure.

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
      // Display a MsgBox asking the user if he is sure to close
      if(MessageBox.Show("Are you sure you want to close?", "My Application", MessageBoxButtons.YesNo) 
         == DialogResult.Yes)
      {
         // Cancel the Closing event from closing the form.
         e.Cancel = false;
         // e.Cancel = true would close the window
      }
}