How to set application shutdown mode in a Windows Forms C # project?

I just switched from VB.Net to C # and wrote a Windows application. In VB.net you can easily change the shutdown mode by selecting the project properties and going to the drop-down menu where you can choose "When the launch form closes" and "When the last form closes". Please help me find the equivalent in C #.

By the way, I am using VS 2010.

+6
source share
2 answers

In C #, the easiest trick for this is to change the entry point of the application in the program.cs file. This input form should be hidden at startup, but will invoke the main form. Then call Application.Exit (); function in the closing procedure in any other form / class.

Pseudo code example 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 more accurate way to manage and track open forms later in your program.

+8
source

This is not the first thing you can find, but if you look at the documents for Application.Run (ApplicationContext) , you will find a code sample to execute what you ask: exit the application when the last form is closed.

+3
source

Source: https://habr.com/ru/post/900750/


All Articles