Sep 3, 2012

How to ignore the key Alt - F4 combination in C#?

1 comment
Alt-F4 is a windows global shortcut which closes an active window.  In some scenarios we might need to restrict this behavior. Eg: Splash screen.

Generally in large desktop applications, before displaying actual application to user, we do some background operations (database loading, copying some files) which takes little longer time. While performing this, instead of showing nothing to user, we will display one screen with company logo or application icon. This should not be closed by end user either way. i.e., using Alt F4 or ESC or using close buttons on form

Here is the simple code snippet which will ignore the Alt + F4 combination.

protected override bool ProcessDialogKey(Keys keyData)
{
    if (keyData == (Keys.Alt | Keys.F4))
return true;

    return base.ProcessDialogKey(keyData);
} 

Similar way, we can restrict any of the keys used by end user as per need.

1 comment :