Question

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.

Was it helpful?

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.

OTHER TIPS

protected override void OnFormClosing(FormClosingEventArgs e)
{
    if(!boolTrackingButtonPressed)
    {
       base.OnFormClosing(e);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top