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

后端 未结 4 824
一个人的身影
一个人的身影 2020-11-22 05:13

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.

Fi

相关标签:
4条回答
  • 2020-11-22 05:18

    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
          }
    }
    
    0 讨论(0)
  • 2020-11-22 05:24

    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());
       }
    }
    
    0 讨论(0)
  • 2020-11-22 05:26

    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;
        }
    
    0 讨论(0)
  • 2020-11-22 05:26

    You can take this way:

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

    Hope it was helpful.

    0 讨论(0)
提交回复
热议问题