我正在开发一个C#应用程序,需要在用户关闭窗体之前进行一些验证操作。
我尝试使用FormClosing事件,但没有成功,后来我使用了FormClosed事件,但结果相同。
问题是,当我点击窗体上方的“关闭按钮”时,它不执行任何操作,但我已经在窗体属性中声明了事件。
以下是我的代码:
private void Inicio_FormClosing_1(object sender, FormClosingEventArgs e)
{
//things I have to do
//...
//...
if(bandera==true)
Application.Exit();
}
并且
private void Inicio_FormClosed_1(object sender, FormClosingEventArgs e)
{
//things I have to do
//...
//...
if(bandera==true)
Application.Exit();
}
有什么想法吗?
谢谢
if(bandera=true)
几乎肯定是错误的。你正在将true
分配给bandera
,然后返回true
。你可能想要写成if(bandera==true)
,不过你可以简化为if(bandera)
,因为任何布尔值与true
进行比较时都会返回它本身。根据你观察行为的方式,这可能是你实际问题的原因,也可能不是。 - ServyInitializeComponent();
这一行。 - Jean