捕捉关闭事件一个C#形式 [英] Catching the close event on a c# form

查看:203
本文介绍了捕捉关闭事件一个C#形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿!我并不想在这里把我的运气,但我还有一个C#问题。
我已经尝试了一切可能的情况下,我发现使用谷歌。
下面是代码:

Hey! I am not trying to push my luck here but I have another c# question. I have tried every possible event I found using google. Here is the code:

 private void Form1_OnClose()
        {
            MessageBox.Show("I was closed -2");
        }

        private void Form1_Exit(object sender, EventArgs evArgs)
        {
            MessageBox.Show("I was closed -1");         
        }
        private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            MessageBox.Show("I was closed 0");
        }     

        private void Form1_Closed(object sender, EventArgs e)
        {
            MessageBox.Show("I was closed 1");
        }
        private void Form1_FormClosed(Object sender, FormClosedEventArgs e)
        {

            MessageBox.Show("I was closed 2");
        }



不属其中之一触发什么时候我要么做Alt + F4键或点击X按钮。
我在做什么错在这里?

Not one of these trigger anything when I either do Alt+f4 or click on the X button. What am I doing wrong here?

推荐答案

您可能是缺少实际认购代码,这是东西沿着这些路线

You might be missing actual subscription code, which is something along these lines:

this.Closing += Form1_Closing;



相反,尝试重写的 OnXXX 的方法 - 这是在做的首选方式的东西。

Instead, try overriding OnXXX methods - this is the preferred way of doing things.

这篇关于捕捉关闭事件一个C#形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆