C# - 标题栏“X”不关闭程序和形式切换
我有一个使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在回答您的问题之前,我应该指出
Hide
实际上并没有关闭您的表单,它只是(顾名思义)隐藏它。因此,随着时间的推移,您将不断堆积表单,直到耗尽 GDI 对象或内存不足,无论哪种方式都会崩溃。不过,您对应用程序未关闭的原因是正确的:即使您关闭当前表单,所有其他表单仍会加载,因此您的应用程序不会结束。
要解决此问题,最好的方法是在不再需要表单时实际关闭它们。您甚至不必添加任何代码来关闭您的应用程序。
现在,如果您出于某种原因不想这样做,您可以随时调用
Application.Exit
。不过,我强烈建议您不要寻求这种“解决方案”。编辑:至于可能的解决方案,您可以将Program.cs更改为:
然后您的
formSwitch
将变为:它看起来很奇怪,因为对于 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:
And then your
formSwitch
would become: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.
默认的 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.您可以按如下方式编辑
~/Program.cs
文件中的代码:这样,在您专门调用
Application.Exit()
之前,应用程序不会关闭。You can edit the code in the
~/Program.cs
file as the following:This way, the application wont close until you call
Application.Exit()
exclusivly.