Solved

Prevent user enter ALT-F4

Posted on 2011-03-25
6
468 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

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

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

911 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now