How to Disable Alt + F4 closing form?

后端 未结 11 1532
遇见更好的自我
遇见更好的自我 2020-11-29 21:23

What is the best way to disable Alt + F4 in a c# win form to prevent the user from closing the form?

I am using a form as a popup dialog to dis

11条回答
  •  失恋的感觉
    2020-11-29 21:54

    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();
    

提交回复
热议问题