Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 992
Hunter Mitchell Avatar asked Sep 03 '25 04:09

Hunter Mitchell


1 Answers

//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.

like image 187
Servy Avatar answered Sep 04 '25 16:09

Servy