문제

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.

도움이 되었습니까?

해결책

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

다른 팁

protected override void OnFormClosing(FormClosingEventArgs e)
{
    if(!boolTrackingButtonPressed)
    {
       base.OnFormClosing(e);
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top