Frage

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.

War es hilfreich?

Lösung

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

Andere Tipps

protected override void OnFormClosing(FormClosingEventArgs e)
{
    if(!boolTrackingButtonPressed)
    {
       base.OnFormClosing(e);
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top