Windows Develop Bookmark and Share   
 index > Windows Forms General > Shutdown mode
 

Shutdown mode

Where and or how can I set the Shutdown mode to When last form closes in C#? I see it as an application setting in VB.NET but its not available in the C# settings. I'm using VS.NET 2008.

Thanks

scromer17  Thursday, February 07, 2008 3:01 AM

Hello,

There is no equivalent of the Shutdown mode in C#. However, if you want to implement the feature "Shutdown when last form closes", we can do it in this way in C#

In C# main method,

Code Snippet

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

List<Form> forms = new List<Form>();

Form1 form1 = new Form1();
forms.Add(form1);
form1.Show();
Form2 form2 = new Form2();
forms.Add(form2);
form2.Show();
Application.Run();

the list<form> forms is used to record all the open forms of the application. Everytime we create a new form, we should add the form into the list.

For each form, we register its OnClosing event. In the event handler, we remove the form itself from the forms list, and check if the forms is empty, if it is empty, we can assert that all the forms have been closed, so we call Application.Exit to shut down the application at the moment.

Hope it helps.

Regards

Jialiang Ge

Jialiang Ge - MSFT  Friday, February 08, 2008 7:47 AM

Hello,

There is no equivalent of the Shutdown mode in C#. However, if you want to implement the feature "Shutdown when last form closes", we can do it in this way in C#

In C# main method,

Code Snippet

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

List<Form> forms = new List<Form>();

Form1 form1 = new Form1();
forms.Add(form1);
form1.Show();
Form2 form2 = new Form2();
forms.Add(form2);
form2.Show();
Application.Run();

the list<form> forms is used to record all the open forms of the application. Everytime we create a new form, we should add the form into the list.

For each form, we register its OnClosing event. In the event handler, we remove the form itself from the forms list, and check if the forms is empty, if it is empty, we can assert that all the forms have been closed, so we call Application.Exit to shut down the application at the moment.

Hope it helps.

Regards

Jialiang Ge

Jialiang Ge - MSFT  Friday, February 08, 2008 7:47 AM

You can use google to search for other answers

Custom Search

More Threads

• passing values within methods in a form
• Activate MenuStrip's ToolStripMenuItem's DropDownItems menu on MouseEnter (C#)
• Alpha Transparency in 2000 with .PNGs
• Resource Linking/Namespace Problems
• Use of Progressbar
• MonthCalendar problem
• Task switcher Icon
• Load MHT files open as stream/string in WebBrowser contol
• Implementing help in touchscreen UI
• C# - how to display taskbar icon for borderless form?