FormClosing 和 FormClosed 事件不起作用

问题描述 投票:0回答:7

我正在开发一个

C#
应用程序 我需要在用户关闭表单之前进行一些验证。

我尝试使用

FormClosing
事件,但没有成功, 后来我用了
FormClosed
事件,但是一样。

问题是,当我单击“关闭按钮”(位于表单顶部)时,它不会执行任何操作,但我在表单属性和所有内容中都有事件。

enter image description here enter image description here

这是我的代码:

    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();

    }

有什么想法吗?

谢谢你

c# forms events
7个回答
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 event
无关(否则,代码将无法编译)。


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 代码(设计器文件)将需要使用这些新方法名称进行更新。

您可以在这些方法中放置断点以查看它们是否被调用。


0
投票

顺便说一句,Form.Hide() 方法不会引发 form_close 或 form_looking 事件


0
投票

我也遇到了类似的问题,它是由使用

Dispose()
引发的。 确保您使用
Close()
引发关闭/关闭事件。


0
投票

对于我来说,如果在 VB.net 中调用 END,FormClosing 将不会触发。必须使用 Me.Close() 来触发事件。

© www.soinside.com 2019 - 2024. All rights reserved.