20

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?

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
jn4kim
  • 399
  • 1
  • 3
  • 10

4 Answers4

41

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;
    }
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
5

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());
   }
}
Anders Forsgren
  • 10,827
  • 4
  • 40
  • 77
-1

You can take this way:

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

Hope it was helpful.

DaveShaw
  • 52,123
  • 16
  • 112
  • 141
Shamal Sabah
  • 229
  • 2
  • 7
  • em.. this is the same as what i wrote on my question. anyways, thanks for answering – jn4kim May 28 '12 at 12:50
  • you have form1 and form 2, when you press a button on form1 to open form2 you want to close form1, if you use form1.Hide(); it will hide form1, why you want to close form1?just hiding is not good for you? – Shamal Sabah May 29 '12 at 13:15
-1

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
      }
}
Jaroslav Kadlec
  • 2,505
  • 4
  • 32
  • 43
nixn
  • 1,337
  • 3
  • 16
  • 33