Solved

Prevent user enter ALT-F4

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

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

Thanks,
Jt
0
Comment
Question by:jtran007
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 30

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
How our DevOps Teams Maximize Uptime

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

 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

752 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