Solved

Prevent user enter ALT-F4

Posted on 2011-03-25
6
469 Views
Last Modified: 2013-12-17
Hi,

How to prevent user close program by using ALT-f4?

Thanks,
Jt
0
Comment
Question by:jtran007
6 Comments
 
LVL 29

Expert Comment

by:Randy Downs
ID: 35216460
Try this - http://stackoverflow.com/questions/1846109/disable-submit-with-key-press-event

It's for the enter key but you just need the code generated by ALT F4 to change it

Javascript code

    function disableEnterKey(e)     {          var key;            if(window.event)               key = window.event.keyCode;     //IE          else               key = e.which;     //firefox           if(key == 13)               return false;          else               return true;     } KeyCode 13 stands for the enter key.

0
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 35216559
Hi, try this code

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)
}
0
 

Author Comment

by:jtran007
ID: 35216560
Hi ,

I use c#. Please give example on c#.

Thanks,
JT
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 5

Expert Comment

by:felipevidaurri
ID: 35218032
Try using FormClosing Event:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    e.Cancel = true;
}
0
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 250 total points
ID: 35218267
Set the Form's KeyPreview to true, and handle the form's KeyDown event. If the KeyPressed is F$ and the ALT key is currently down, then cancel the KeyPress by telling .Net we handled the key press, so it doesn't need to.  This will leave the user still able to close the form with the mouse, but not by pressing ALT-F4.

public partial class Form1 : Form
{
	public Form1()
	{
		InitializeComponent();
	}

	private void Form1_KeyDown(object sender, KeyEventArgs e)
	{
		if (e.Alt && e.KeyCode == Keys.F4)
			e.Handled = true;
	}

		

}

Open in new window

0
 

Author Closing Comment

by:jtran007
ID: 35269002
Thanks,
JT
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question