'How to disable Alt+F4 in a WinForms application

I'd like to disable Alt+F4 in a WinForms app. Where do I need to add the code?

namespace FileName
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}


Solution 1:[1]

you can do this if you want to prevent all user close operations (click on form close X in the corner for example as well as AltF4)

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

Solution 2:[2]

Add the following code to the FormClosing event: This does not affect you using other methods to close the window.

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

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 pm100
Solution 2