简体   繁体   English

如何绕过FormClosing事件

[英]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. 我希望上面的按钮关闭表单而不隐藏它。

//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". 另一种选择是使布尔字段为“ shouldIReallyClose”。 Default it to false. 默认为false。 When you really want to close the form set it to true. 当您确实要关闭表单时,请将其设置为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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM