Home > database >  How to disable Alt F4 in a WinForms application
How to disable Alt F4 in a WinForms application

Time:03-02

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)
        {

        }
    }
}

CodePudding user response:

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;
     }

CodePudding user response:

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

  • Related