FormClosing和FormClosed事件不起作用。

6
我正在开发一个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();

    }

有什么想法吗?

谢谢


4
请注意,表达式if(bandera=true)几乎肯定是错误的。你正在将true分配给bandera,然后返回true。你可能想要写成if(bandera==true),不过你可以简化为if(bandera),因为任何布尔值与true进行比较时都会返回它本身。根据你观察行为的方式,这可能是你实际问题的原因,也可能不是。 - Servy
@Jean:在函数开头设置一个断点,然后查看它的事件并捕获它! - KF2
@Servy 我编辑了它!抱歉(但这不是问题) - Jean
1
我发现了我的错误,谢谢你 @Brian。我不小心删除了 InitializeComponent(); 这一行。 - Jean
@Jean,很高兴听到你解决了它。 - Brian Rogers
显示剩余3条评论
6个回答

5

两个事件都应该正常工作。只需打开一个新项目并进行以下简单测试:

 private void Form1_Load(object sender, EventArgs e)
 {
     this.FormClosing += new FormClosingEventHandler(Inicio_FormClosing_1);
     this.FormClosed += new FormClosedEventHandler(Inicio_FormClosed_1);
 }

 private void Inicio_FormClosing_1(object sender, FormClosingEventArgs e)
 {
     //Things while closing

 }

 private void Inicio_FormClosed_1(object sender, FormClosedEventArgs e)
 {
     //Things when closed
 }

如果您在这些方法中设置断点,您会发现它们是在点击关闭按钮后被触发的。似乎在您的事件绑定代码中存在一些问题。例如:Inicio_FormClosed_1(object sender, FormClosingEventArgs e) 是错误的,因为它应该接受一个FormClosedEventArgs参数;因此,这个方法肯定不与FormClosed事件相关联(否则,代码将无法编译)。

我做到了,它能工作!但是在我的应用程序中却不能工作。为什么?@varocarbas - Jean
@Jean,我也不明白这个(处理这些事件时从未遇到过问题;我通常处理FormClosing事件,正如上面建议的那样)。请查看您附加事件的代码部分,并查看可能存在的问题。 - varocarbas
它能够工作是因为这段代码将你的方法链接到了FormClosingEventHandler。这通常不会在窗体加载时完成,通常你需要通过属性窗口在UI中完成此操作。@jean - L_7337

5

我找到了错误;

这里:(当我初始化表单时)

    public Inicio()
    {
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.AutoScroll = true;

        this.ClientSize = new System.Drawing.Size(635, 332);
        this.StartPosition = FormStartPosition.CenterScreen;
        llenaForm(nombreFormulario);
        Application.EnableVisualStyles();

    }

我需要的只是:InitializeComponent();
我不小心把它删除了。

应该是这样的:

    public Inicio()
    {
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.AutoScroll = true;`

        InitializeComponent();//<<<<<<<<------------------- 

        this.ClientSize = new System.Drawing.Size(635, 332);
        this.StartPosition = FormStartPosition.CenterScreen;
        llenaForm(nombreFormulario);
        Application.EnableVisualStyles();
    }

非常感谢大家!

3
为了防止用户在某些验证情况下关闭表单,您需要设置FormClosingEventArgs.Cancel = true
例如:
private void Inicio_FormClosing_1(object sender, FormClosingEventArgs e)
{
    if (string.IsNullOrEmpty(txtSomethingRequired.Text))
    {
        MessageBox.Show("Something is required here!");
        if (txtSomethingRequired.CanFocus) txtSomethingRequired.Focus();
        e.Cancel = true;
        return;
    }
}

为了防止窗体关闭,您只能在 FormClosing 事件中进行验证;如果等到 FormClosed ,那就太晚了。


1
我发现您的方法名称末尾有一个"_1"。您是否已重命名这些方法?如果是这样,您的UI代码(设计器文件)需要使用这些新方法名称进行更新。您可以在这些方法中设置断点以查看它们是否被调用。

我有一些断点,但这些方法没有被调用 @L_7337 或者完全被省略了。 - Jean
2
我的猜测是要么调用了Application.Exit(),要么FormClosing事件处理程序没有正确链接。 - L_7337
1
选中窗体后,进入属性窗口并点击闪电图标。滚动到FormClosing并确保它是“Inicio_FormClosing_1”。 - L_7337
没关系,但问题仍然存在 @L_7337 - Jean

0
只是顺便提一下,Form.Hide() 方法不会触发 form_closed 或 form_closing 事件。

0

我也遇到了类似的问题,而且是由于使用了Dispose()引起的。 请确保你使用Close()来触发关闭/已关闭事件。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接