Solved

Prevent user enter ALT-F4

Posted on 2011-03-25
6
463 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 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

15 Experts available now in Live!

Get 1:1 Help Now