English 中文(简体)
当我关闭启动表格时, 我如何防止应用程序终止?
原标题:How do I prevent the app from terminating when I close the startup form?
  • 时间:2012-05-26 19:36:22
  •  标签:
  • c#
  • forms

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.

首先,我试过

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

但有一个缺点,即表格2关闭时申请没有退出。所以,我不得不在表格2表格中填入更多表格来源_表格关闭部分。

我想知道这是否是正确的方法...

最佳回答

程序. cs 中自动生成的代码是在启动窗口关闭时终止应用程序的写入。 您需要调整它, 只有当不再有窗口时它才会终止。 例如 :

    [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;
    }
问题回答

默认情况下,第一个窗体控制 Windows 窗体应用程序的寿命。如果需要多个独立的窗口,您的应用程序上下文应该是与窗体分开的上下文。

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

要使用上下文, 请将其传送到 application. Run( 而不是窗体) 。 如果您想要创建另一个表单 1, 请调用 MyContext.ShowForm1 () 等 。

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

你可以这样走:

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

希望有帮助

写入您在 FormClosing 事件发生时执行的方法 。

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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签