c# - 视觉 C# : Exit the Program Check

标签 c# exit

在 Visual C# 中,如何检测用户是否单击 X 按钮关闭程序?我想询问用户是否愿意在退出前执行某个操作。我的程序本身有一个退出按钮,我知道我可以按如下方式对其进行编码:

    private void exitToolStripMenuItem_Click(object sender, EventArgs e)
    {
        DialogResult result;
        if (logfiletextbox.Text != "")
        {
            result = MessageBox.Show("Would you like to save the current logfile?", "Save?", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
            if (result == DialogResult.Yes)
            {
                savelog.PerformClick();
            }
        }
        Environment.Exit(0); //exit program
    }

但是我该如何为程序中已经内置的 X 按钮执行此操作?

最佳答案

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (MessageBox.Show("cancel?", "a good question", MessageBoxButtons.YesNo) == DialogResult.Yes)
        {
            e.Cancel = true;
        }
    }

这是“FormClosing”——表单的事件。玩得开心, friend 。

关于c# - 视觉 C# : Exit the Program Check,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3213296/

相关文章:

C# 程序集绑定(bind)重定向 - Newtonsoft.Json

c# - 使用 C# 从另一个窗体获取值

c# - 使用 LINQ 按一个字段分组,基于组求和并存储在字典中

c# - 使用Application.Exit退出应用程序而不显示GUI

python - 用户输入 Exit 以中断 while 循环

c++ - QT:如何退出应用程序并关闭用户界面

c# - Win 应用程序开发所需的获取 (407) 代理身份验证

c# - 未经授权的错误机器人框架

Android应用程序退出,使用哪种机制?

python - 如何删除Python IDLE中的exit(0)弹出窗口?