简体   繁体   中英

How to Set Application Shutdown Mode in a C# Windows Forms Project?

I have just switched from VB.Net to C# and am writing a windows application. In VB.net you could easily change the Shutdown Mode by selecting the properties of the project and moving to a dropdown where you could choose between "When startup form closes" and "When last form closes". Please help me to find the equivalent in C#.

By the way, I'm using VS 2010.

In c# the easiest trick to achieve this is to change the entry point of the application in the "program.cs" file. This entry form should be hidden on startup,but will call the main form. Then call the Application.Exit(); function in the close procedure in any other form/class.

Sample pseudo code below

program.cs

//edit this line
Applcation.Run(startupForm);

StratupForm.cs

//startup method
StartupForm_load (object e)
{
this.Hide();
MainForm mainForm = new MainForm();
mainForm.show();
}

MainForm.cs

 //application exit method
    MainFormExit_close (object e)
    {
     Application.Exit();
    }

You should probably implement a neater way to manage and keep track of open forms later in your program

这不是你可能想要的第一件事,但如果你查看Application.Run(ApplicationContext)的文档,你会发现样本代码完全符合你的要求:当最后一个表单关闭时退出应用程序。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM