prevent closing with Alt + F4

  • Thread starter Thread starter Guest
  • Start date Start date
Well, you could override the KeyDown/KeyPress and ignore those
combinations, or handle the OnClosing event and check for what kind of
closing is performed

protected override void OnClosing(CancelEventArgs e)
{
System.Diagnostics.StackTrace O = new System.Diagnostics.StackTrace(true);
System.Diagnostics.StackFrame F = O.GetFrame(7);
if(F.GetMethod().Name == "DefWndProc")
e.Cancel = true; // user ended the application (ALT-F4, clicking the
corner X etc)
}
 
Thanks a lot Morten. But i don't understand a single line of code.
The thing is, i'm just a vb.net rookie. But thanks anyhow.
 
The VB.NET code should be very similar. The StackTrace class is used for
debugging purposes as it contains a trace of methods called in your
application put into StackFrames, the 8th last StackFrame will reveal what
triggered the OnClosing event so we retrieve the StackFrame and compare
the Method Name with known values like "DefWndProc" = User ended the
application by means of ALT-F4 etc, "SendMessage" = this/Me.Close() was
called from within your application, "DispatchMessageW" = User ended the
application by means of the TaskManager (aborting process) etc.
 
Back
Top