473,322 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 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 7131
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: kishor kotecha | last post by:
Hi, I have created a .net app. all works fine. but, i want to prevent closure (kill) of this app when the user of the machine does a 'log off' of windows/xp. currently what is happening...
22
by: alecjames1 | last post by:
I have a form which the user must complete before closing. I have disabled the window x button and use my own exit button. When selected it checks to see if the user has completed the entries...
7
by: alan | last post by:
Hello, When the user try the close the application (click the top right side button "X"), I would like to do a checking first. If the checking return false, prevent the application terminate....
4
by: Daniel Walzenbach | last post by:
Hi, does anybody know the JavaScript the guys at Microsoft used in MS CRM to prevent people from closing a browser window and asking them (on the client) what they really want to do? If you...
9
by: Daniel Walzenbach | last post by:
Hi I am faced with the following problem: I have a page (let’s call this page page1.aspx) containing some TextBoxes and a hyperlink which opens another page (let’s call this page page2.aspx)...
2
by: Snuyt | last post by:
Hallo, when a form is closed by clicking the closing button (cross in the right upper corner of the form), it is also disposed. Can I prevent this, and instead of closing (and disposing) the...
2
by: kevininstructor | last post by:
I want to prevent a user from closing an application while doing critical operations. The following code (concept came from MSDN) works except for when the user attempts to terminate via "Task...
4
by: tlyczko | last post by:
I have been looking on the NGs and I found this code to show if a subform has no records. I have this code in the MAIN form OnClose event: Private Sub btnClose_Click() '4/16/06 new code that...
3
by: sravan_reddy001 | last post by:
i want to prevent a form from closing.. to do this i want to handle the formClosing or FormClosed events. from here i want to prevent the form from closing. New instance of same form should...
7
by: Michiel Rapati-Kekkonen | last post by:
Hi, I would like my record to be saved only when my own close button (with all it's checks) is used. I made the winodws close button disappear, but you still can close using the right mouse...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.