Solved

Prevent user enter ALT-F4

Posted on 2011-03-25
6
470 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
DevOps Toolchain Recommendations

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

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Upgrading my SSIS package in VS 2012 6 83
Vb.net threads keep increasing 2 48
C# Windows app updating JSON file error. 9 38
C# XML Get Values 4 33
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 …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

680 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