Search code examples
c#.netwinformsformclosing

How to bypass FormClosing event


I have the following code:

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

I want a button that closes the form, without triggering this event. How do I do this?

The code for the button:

private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
    Application.Exit(); 
}

I want the above button to close the form and not hide it.


Solution

  • //unsubscribe the mean "don't let me close" handler
    this.FormClosing -= form1_closing; 
    //close form
    this.Close();
    

    Another option would be to make a boolean field "shouldIReallyClose". Default it to false. When you really want to close the form set it to true. Change the event handler to check that boolean before canceling the event.