在Windows窗体覆盖标准关闭(X)按钮 [英] Override standard close (X) button in a Windows Form

查看:145
本文介绍了在Windows窗体覆盖标准关闭(X)按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何去改变,当用户单击一个Windows的关闭(红色的X)按钮,会发生什么情况窗体应用程序(在C#)?

How do I go about changing what happens when a user clicks the close (red X) button in a Windows Forms application (in C#)?

推荐答案

您可以覆盖 OnFormClosing 以做这个。只是要小心,你不要做任何事情太出乎意料,因为点击X关闭是一个很好理解的行为。

You can override OnFormClosing to do this. Just be careful you don't do anything too unexpected, as clicking the 'X' to close is a well understood behavior.

protected override void OnFormClosing(FormClosingEventArgs e)
{
    base.OnFormClosing(e);

    if (e.CloseReason == CloseReason.WindowsShutDown) return;

    // Confirm user wants to close
    switch (MessageBox.Show(this, "Are you sure you want to close?", "Closing", MessageBoxButtons.YesNo))
    {
    case DialogResult.No:
        e.Cancel = true;
        break;
    default:
        break;
    }        
}

这篇关于在Windows窗体覆盖标准关闭(X)按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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