要关闭C#窗体应用程序窗体、应用程序

2023-09-03 04:52:09 作者:┣Ч惡犬のταο索

我有2种形式......当我开始application..and使用从标题中的关闭X栏的整个应用程序关闭......现在,当我选择在我的情况下,第一个形式,它是一种选择因为我已经使用1stform.hide()和2ndform.show(按钮ADD为电话簿application..it进入到第二个表格)......现在,当我做X在标题栏中它不关机完全为1stform未闭....如何将其编程以这样的方式谓任何阶段,整个应用程序应该关闭

I have 2 forms ...when i start the application..and use the close "X" from the title bar the entire application closes...now when i select an option from the 1st form in my case it is a button "ADD" as its a phonebook application..it goes to the 2nd form as i have used 1stform.hide() and 2ndform.show()...now when i do "X" from the title bar it doesnt shutdown completely as the 1stform is not closed....how to program it in such a way tht any stage the entire application should close

推荐答案

您第一种形式被设置为启动窗体。这意味着,只要它被关闭,整个应用程序是封闭的。反之,你的应用程序没有关闭的到的它被封闭。所以,当你隐藏的启动窗体,并显示第二种形式,关闭第二表单的用户不会触发您的应用程序关闭,因为他们只关闭了次要的,无模式对话框。

Your first form is set as the startup form. That means whenever it gets closed, your entire application is closed. And conversely, your application does not close until it gets closed. So when you hide the startup form and show the second form, the user closing the second form does not trigger your application closing because they have only closed a secondary, non-modal dialog.

我建议改变你的设计,使启动窗体也就是主的形式应用程序。无感试图解决的内置功能,实际上可能是有用的。你要当主窗体关闭,不管其他子窗体打开应用程序退出。

I recommend changing your design so that the startup form is also the main form of your application. No sense trying to work around built-in functionality that can actually be useful. You want the application to quit when the main form is closed, no matter what other child forms are opened.

但在你的情况下,快速和肮脏的解决办法是拨打电话为 Application.Exit.这将关闭的所有的当前打开表单,并立即退出你的应用程序。正如我所说的正上方,我也没有那么多推荐这种方式,因为不必调用 Application.Exit 从各种形式的FormClosed事件处理程序是一个迹象,表明事情已经严重错误在您的设计。

But the quick-and-dirty solution in your case is to make a call to Application.Exit. That will close all of the currently open forms and quit your application immediately. As I said just above, I don't so much recommend this approach because having to call Application.Exit from every form's FormClosed event handler is a sign that something has gone seriously wrong in your design.

如果单启动窗体模式不工作了你,你应该考虑考虑事项到你自己的手中,并自定义方法,在你的的Program.cs 源文件。看到答案给this相关的问题一些想法如何可能为你工作。

If the single startup form paradigm doesn't work out for you, you should look into taking matters into your own hands and customizing the Main method in your Program.cs source file. See the answers given to this related question for some ideas on how that might work for you.