开发者

C# - Title Bar "X" does not close the program AND form switching

I have a Visual C# project using windowsforms that does not exit when the X is clicked on any form OTHER than the first form. I think this may have something to do with my form switching?

Currently, I have a Template.CS which is exactly what it sounds开发者_C百科 like. All of my usage forms extend this by:

public partial class Welcome : ADL.template

Then, I switch between forms by invoking this method:

    public static void formSwitch(Form in_form, Form out_form)
    {
        in_form.Hide();
        out_form.Show();          
    }

Called by:

Program.formSwitch(this, new frmUserInput());

What i think is happening here is, the X is closing the Form NOT the application because the starting form is Hidden, not closed. Is there a better way for me to switch between forms?

Thanks!


Well before answering your question, I should point out that Hide doesn't actually close your form, it only (as the name implies) hides it. So as time goes on, you'll keep piling on forms until you either run out of GDI objects or out of memory, either way you'll crash.

You are kind of correct about the reason why your application isn't closing though: even though you close the current form, all your other forms are still loaded so your application won't end.

To fix this, the best way would be to actually close your forms when you don't need them anymore. You won't even have to add any code to close your application then.

Now if you don't want to do that for whatever reason, you can always just call Application.Exit. I strongly discourage you to pursue this "solution" though.

Edit: as for a possible solution, you could change Program.cs to something like:

static class Program
{
  static Form NextForm=new frmLogin();   // or whatever your first form is
  static public void SetNext(Form next) { NextForm=next; }
  static void Main()
  { 
    while(NextForm!=null)
    {
      Form _next=NextForm;
      NextForm=null;      // so it closes at the end
      Application.Run(NextForm);
    }
  }
}

And then your formSwitch would become:

public static void formSwitch(Form in_form, Form out_form)
{
    Program.SetNext(out_form);
    in_form.Close();
}

It looks weird because your workflow is weird for a Windows program. This is more the workflow of a 1970 FORTRAN program running in DOS.


The default Windows Forms application behaviour is: the application is closed when the MAIN window is closed.

The main window is the first one you've created. When you called Hide() on this window, it is rendered invisible (but still exists). So, closing the second window doesn't close the application.


You can edit the code in the ~/Program.cs file as the following:

static class Program
{

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Form f = new Form();
        f.Show();

        Application.Run();
    }
}

This way, the application wont close until you call Application.Exit() exclusivly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜