C# - 标题栏“X”不关闭程序和形式切换

发布于 2024-11-06 15:54:16 字数 574 浏览 1 评论 0原文

我有一个使用 windowsforms 的 Visual C# 项目,当在第一个表单以外的任何表单上单击 X 时,该项目不会退出。我想这可能和我的形态转换有关系?

目前,我有一个 Template.CS,正如它听起来的那样。我的所有使用表单都通过以下方式扩展此功能:

public partial class Welcome : ADL.template

然后,我通过调用此方法在表单之间切换:

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

调用者:

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

我认为这里发生的是,X 正在关闭表单而不是应用程序,因为起始表单是隐藏的,而不是关闭的。有没有更好的方法让我在表单之间切换?

谢谢!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

格子衫的從容 2024-11-13 15:54:16

在回答您的问题之前,我应该指出 Hide 实际上并没有关闭您的表单,它只是(顾名思义)隐藏它。因此,随着时间的推移,您将不断堆积表单,直到耗尽 GDI 对象或内存不足,无论哪种方式都会崩溃。

不过,您对应用程序未关闭的原因是正确的:即使您关闭当前表单,所有其他表单仍会加载,因此您的应用程序不会结束。

要解决此问题,最好的方法是在不再需要表单时实际关闭它们。您甚至不必添加任何代码来关闭您的应用程序。

现在,如果您出于某种原因不想这样做,您可以随时调用 Application.Exit。不过,我强烈建议您不要寻求这种“解决方案”。

编辑:至于可能的解决方案,您可以将Program.cs更改为:

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

然后您的formSwitch将变为:

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

它看起来很奇怪,因为对于 Windows 程序来说,你的工作流程很奇怪。这更像是 1970 年在 DOS 中运行的 FORTRAN 程序的工作流程。

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.

灵芸 2024-11-13 15:54:16

默认的 Windows 窗体应用程序行为是:当主窗口关闭时,应用程序也随之关闭。

主窗口是您创建的第一个窗口。当您在此窗口上调用 Hide() 时,它会变得不可见(但仍然存在)。因此,关闭第二个窗口不会关闭应用程序。

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.

羁客 2024-11-13 15:54:16

您可以按如下方式编辑 ~/Program.cs 文件中的代码:

static class Program
{

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

        Application.Run();
    }
}

这样,在您专门调用 Application.Exit() 之前,应用程序不会关闭。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文