471,338 Members | 1,012 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

prevent closing with Alt + F4

How do I make sure my program cannot be closed by using Alt + F4
--
Thanks for reading
Jul 21 '05 #1
3 7004
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)
}
--
Happy Coding!
Morten Wennevik [C# MVP]
Jul 21 '05 #2
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.

"Morten Wennevik" wrote:
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)
}
--
Happy Coding!
Morten Wennevik [C# MVP]

Jul 21 '05 #3
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.
On Tue, 15 Mar 2005 08:39:08 -0800, waisty vb.net rookie
<wa***************@discussions.microsoft.com> wrote:
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.

"Morten Wennevik" wrote:
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)
}
--
Happy Coding!
Morten Wennevik [C# MVP]


--
Happy Coding!
Morten Wennevik [C# MVP]
Jul 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

22 posts views Thread by alecjames1 | last post: by
7 posts views Thread by alan | last post: by
9 posts views Thread by Daniel Walzenbach | last post: by
2 posts views Thread by Snuyt | last post: by
2 posts views Thread by kevininstructor | last post: by
3 posts views Thread by sravan_reddy001 | last post: by
7 posts views Thread by Michiel Rapati-Kekkonen | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.