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.CloseReason
to 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 at TaskManagerClosing
as well (if someone is trying to close the app with the taskmanager, they probably really want it to close rather than just minimize).
I found another solution for the problem. You can disable the closing button of the form, so your FormClosing event can be handled the default way
Code to disable the closing button:
protected override CreateParams CreateParams
{
get
{
CreateParams param = base.CreateParams;
param.ClassStyle = param.ClassStyle | 0x200;
return param;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With