473,416 Members | 1,656 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,416 software developers and data experts.

i want to prevent windows to close my form when Alt+f4 is pressed

Hi,
Can anybody help me, i want to prevent windows to close my winform of
..NET application, when user presses Alt+F4

Nov 17 '05 #1
7 7839
You could Handle the Closing event th following way:

private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
e.Cancel=true;
}

"Nikki" wrote:
Hi,
Can anybody help me, i want to prevent windows to close my winform of
..NET application, when user presses Alt+F4

Nov 17 '05 #2
....Great that prevents the close (I might add *always* too!) but hardly
helps Nikki with the solution, of which the crux of it is detecting the key
events in the first place.

***Nikki*** There are possibly more elegant ways to do this (I can think of
at least one other) but they will all essentially use the following
detection. Your form will have one private var that indicates whether the
form should be allowed to close or not. You detect the press and set this
var to disallow. Then in the forms (because it will still fire) closing
event you should read this var, and allow close or not. Obviously if close
is not allowed and doesnt happen then the var should then be reset to allow
close for any future events.

The code snippet is as follows (if you want me to include the class file
then let me know). Might I also suggest that rather than preventing the
close you should handle the close i.e. provide a prompt to the user and if
they say OK then cleanup. The reason I say this is because ALT+F4 is (as you
know) known and expected behaviour -and you are taking that away (IMHO I
would say that is poor design). Personally it would annoy the hell out of
me.

private bool enableClose = true;

private void Form1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.F4 && e.Alt)
{
MessageBox.Show("Sorry you cannot close this form via ALT+F4");
enableClose = false;
}
}

private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
if (enableClose == false)
{
enableClose = true;
e.Cancel = true;
}
}

br,

Mark.
"Marinus Holkema" <Ma************@discussions.microsoft.com> wrote in
message news:74**********************************@microsof t.com...
You could Handle the Closing event th following way:

private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
e.Cancel=true;
}

"Nikki" wrote:
Hi,
Can anybody help me, i want to prevent windows to close my winform of
..NET application, when user presses Alt+F4

Nov 17 '05 #3


Thank you Mark. It's a gr8 help, U saved lot of my time & effort.
*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #4
Absolutely no problem at all :-)
In fact you might be able to help me, if you know of any C# jobs in my
vacinity (Cambs UK) then let me know....I'm on the hunt!

"Nikki V" <ap*********@gmail.com> wrote in message
news:e8**************@TK2MSFTNGP12.phx.gbl...


Thank you Mark. It's a gr8 help, U saved lot of my time & effort.
*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #5
The problem with this solution is that it prevents windows closing the program when it shuts down as well, and thereby prevents shutting down the computer.

You can detect what triggered the Closing event by parsing the 7th last StackFrame

protected override void OnClosing(CancelEventArgs e)
{
// frames 1-6 will be identical
StackTrace st = new StackTrace(7);
StackFrame sf = st.GetFrame(0); // 7th frame

switch(sf.GetMethod().Name)
{
case "DefWndProc":
// user pressed Alt-F4 or clicked the X button
break;
case "SendMessage":
// something in the code called Close()
break;
}
}

Another problem with this is that Windows itself uses DefWndProc for closing programs at ShutDown as well. You need to set a flag in DefWndProc that you can use inside the Closing event.

The procedure for this is as follows:

private bool shutdown = false;
protected override void DefWndProc(ref Message m)
{
const int WM_QUERYENDSESSION = 0x0011;
if(m.Msg == WM_QUERYENDSESSION)
shutdown = true;
base.DefWndProc (ref m);
}

Inside the Closing event, if shutdown is true, don't cancel the closing event.

In theory this should work, but in my test cases Windows never sent the WM_QUERYENDSESSION message. It may work for you though.
--
Happy coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #6
Mark Broadbent wrote:
private void Form1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.F4 && e.Alt)
{
MessageBox.Show("Sorry you cannot close this form via ALT+F4");
enableClose = false;
}
}


Don't you have to set KeyPreview = true on the form for this to work?
Otherwise the KeyDown event gets fired in the "control-with-focus" and the
form doesn't see it but gets closed anyway.

Another idea would be to set e.Handled = true in the KeyDown event when you
detect ALT-F4. This way the key simply gets ignored and you don't need to
check enableClose in the Closing method.

Just my 2c.. I needed the same just a few days ago.

Max

Nov 17 '05 #7
Your post is of course completely correct, well spotted! Wrote my code in a
few seconds so missed those two things ;-)

"Markus Stoeger" <sp******@gmx.at> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Mark Broadbent wrote:
private void Form1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.F4 && e.Alt)
{
MessageBox.Show("Sorry you cannot close this form via ALT+F4");
enableClose = false;
}
}


Don't you have to set KeyPreview = true on the form for this to work?
Otherwise the KeyDown event gets fired in the "control-with-focus" and the
form doesn't see it but gets closed anyway.

Another idea would be to set e.Handled = true in the KeyDown event when
you
detect ALT-F4. This way the key simply gets ignored and you don't need to
check enableClose in the Closing method.

Just my 2c.. I needed the same just a few days ago.

Max

Nov 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: waisty vb.net rookie | last post by:
How do I make sure my program cannot be closed by using Alt + F4 -- Thanks for reading
0
by: Ron James | last post by:
I have a Dialog type form that starts a worker thread, allows the thread to update its progess and provides a Cancel button. Following advice from Chad Meyers in a recent posting, the worker...
3
by: Stefan | last post by:
Hy, i have an app and i must disable this combination: ALT+F4; CTRL+ALT+DEL; CTRL+ESC;ALT+TAB like this: i find something on Internet and i can block ALT+F4 protected override...
3
by: 007 | last post by:
I have a FixedToolWindow form that I create and display when the user presses a button on the toolbar. I want to prevent Alt+F4 on the FixedToolWindow form. How do I accomplish this? I want the...
2
by: cashdeskmac | last post by:
I have written a small program and I want the user to close it down solely by clicking on a button. How can I disable the ALT+F4 method of closing the application down?
7
by: Michael Maes | last post by:
Hello, ALT+F4 is the shortcut to close a form. When I use this shortcut in an MDI enviroment, the application is closed, so obviously the shortcut applies to the 'Container' and not to the...
2
by: RTT | last post by:
<form id="Form1" method="post" runat="server"> <asp:textbox id="TxtEmail" tabIndex="1" runat="server" Width="272px" ToolTip="Enter your emailaddress for authentication"></asp:textbox> <asp:textbox...
5
by: freelancex | last post by:
Hi, Im new to this forum, and new to scripting in general. Im an ICT Technician for a high school and i am designing a Form in visual basic 2005. The intended purpose of the Form, is to prevent...
9
by: raknin | last post by:
Hi, I want to detect when the user close a window through the X button or ALT-F4 or when the user go to another page and leave my page. I try to use the onuload and on unloadbeore but it...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.