How to Disable Alt + F4 Closing Form

How to Disable Alt + F4 closing form?

This does the job:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
}

Edit: In response to pix0rs concern - yes you are correct that you will not be able to programatically close the app. However, you can simply remove the event handler for the form_closing event before closing the form:

this.FormClosing -= new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
this.Close();

How do prevent any Form from closing using alt + F4

Handle Atl+F4 by your self and set it handled.

In the form constructor first set

this.KeyPreview = true;

Then handle the keyDown event

 private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Alt && e.KeyCode == Keys.F4)
{
e.Handled = true;
}

}

Disable Alt+F4 but allow the form to be closed by code, CloseReason.UserClosing is not helping

If you need to filter out Alt + F4 event only (leaving clicking of close box, this.Close() and Application.Exit() to behave as usual) then I can suggest the following:

  1. Set form's KeyPreview
    property to true;
  2. Wire up form's FormClosing and KeyDown events:

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
    if (_altF4Pressed)
    {
    if (e.CloseReason == CloseReason.UserClosing)
    e.Cancel = true;
    _altF4Pressed = false;
    }
    }

    private bool _altF4Pressed;
    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
    if (e.Alt && e.KeyCode == Keys.F4)
    _altF4Pressed = true;
    }

How to prevent ALT+F4 of a WinForm but allow all other forms of closing a WinForm?

Answer to your comment, handling KeyDown from a separate class.

Documentation:

  • AddHandler statement

  • Shared access modifier

Public NotInheritable Class MainInterface
Private Sub New() 'No constructor.
End Sub

Public Shared Sub DisableAltF4(ByVal TargetForm As Form)
TargetForm.KeyPreview = True
AddHandler TargetForm.KeyDown, AddressOf Form_KeyDown
End Sub

Private Shared Sub Form_KeyDown(sender As Object, e As KeyEventArgs)
e.Handled = (e.Alt AndAlso e.KeyCode = Keys.F4)
End Sub
End Class

Now in every form's Load event handler you can do:

Private Sub yourForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MainInterface.DisableAltF4(Me)
End Sub

As Olaf said you can also make all forms inherit from a base class. However this might get a little bit more complicated as you have to tell both the yourForm.vb and the yourForm.Designer.vb file that you want to inherit from the base form.

Public Class BaseForm
Inherits Form

Protected Overrides Sub OnLoad(e As System.EventArgs)
MyBase.OnLoad(e)
Me.KeyPreview = True
End Sub

Protected Overrides Sub OnKeyDown(e As System.Windows.Forms.KeyEventArgs)
MyBase.OnKeyDown(e)
e.Handled = e.Handled OrElse (e.Alt AndAlso e.KeyCode = Keys.F4)
End Sub
End Class

In yourForm.vb:

Public Class yourForm
Inherits BaseForm

...code...
End Class

In yourForm.Designer.vb:

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class yourForm
Inherits yourNamespace.BaseForm

...code...
End Class

VB.NET- Disabling Alt + F4 From Closing My Application

To stop the app from closing, use the form closing event. It will prevent the form from closing for any reason:

Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
If (e.CloseReason = CloseReason.UserClosing) Then
e.Cancel = True
End If
End Sub

To ext the application using the X key, use keydown event

Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.X Then
Application.Exit
End IF
End Sub

how to disable alt+F4 for the application?

You could put something like this in the main startup method:

namespace WindowsFormsApplication1
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.AddMessageFilter(new AltF4Filter()); // Add a message filter
Application.Run(new Form1());
}
}

public class AltF4Filter : IMessageFilter
{
public bool PreFilterMessage(ref Message m)
{
const int WM_SYSKEYDOWN = 0x0104;
if (m.Msg == WM_SYSKEYDOWN)
{
bool alt = ((int)m.LParam & 0x20000000) != 0;
if (alt && (m.WParam == new IntPtr((int)Keys.F4)))
return true; // eat it!
}
return false;
}
}
}

Disable Alt + F4 in ShowDialog()

From How can I prevent a user from closing my C# application?, you can just do this:

protected override CreateParams CreateParams
{
get
{
const int CS_NOCLOSE = 0x200;

CreateParams cp = base.CreateParams;
cp.ClassStyle |= CS_NOCLOSE;
return cp;
}
}

No need to track the keys or cancel the closing in the FormClosing event. Also the close button in the corner will be disabled, but you can still close the form with this.Close();

Disabling Alt+F4 in a Powershell form

Set forms keypreview to true

$form1_KeyDown=[System.Windows.Forms.KeyEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.KeyEventArgs]

if ($_.Alt -eq $true -and $_.KeyCode -eq 'F4') {
$script:altF4Pressed = $true;
}
}

$form1_FormClosing=[System.Windows.Forms.FormClosingEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.FormClosingEventArgs]

if ($script:altF4Pressed)
{
if ($_.CloseReason -eq 'UserClosing') {
$_.Cancel = $true
$script:altF4Pressed = $false;
}
}
}


Related Topics



Leave a reply



Submit