简体   繁体   中英

How do I prevent the app from terminating when I close the startup form?

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?

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;
    }

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());
   }
}

You can take this way:

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

Hope it was helpful.

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
      }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM