I have a simple Form-Based .NET application. In this I capture the FormClosing Event to prevent the closing of the application, instead I minimize it. The application should always be open. Here is the code I use:
private void Browser_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = true;
}
The problem now is that this prevents the computer from shuting down for users with non-admin rights. Anything I can do so that the computer is able to shut down and the user can`t close the application?
Change it so that it doesn’t always cancel but rather first look at
e.CloseReasonto see why the event is being called.See the MSDN page for the CloseReason enumeration for valid values. It includes one called
WindowsShutDown, and you might want to look atTaskManagerClosingas well (if someone is trying to close the app with the taskmanager, they probably really want it to close rather than just minimize).