阻止Windows关机后取消选项C# [英] Prevent windows Shutdown with CANCEL option C#

查看:320
本文介绍了阻止Windows关机后取消选项C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的Windows窗体应用程序,包括当用户试图关闭窗口将打开与取消选项对话框的方式....那个办公室的经典之一。



在其他议题,人们介绍如何防止Windows关机。他们用一个对话框来执行此。它帮助,但如果用户的立即在此框中的任何选项点击,窗口关闭应用程序。



您可以明白我的意思,做以下测试:



在Windows Vista或7打开画图,Word或软的办公室,开始写东西。不要保存它。



尝试关闭Windows和经典的保存对话框出现时的立即点击取消。



您将看到应用程序继续工作和窗户是问你你想要做什么。



我试图按照这个的微软链接但如果我点击确定在消息框中,应用程序关闭。


解决方案

 私人无效Form1_FormClosing(对象发件人,FormClosingEventArgs E)
{
如果(e.CloseReason.Equals(CloseReason.WindowsShutDown))
$ { b $ b如果(MessageBox.Show(你关闭此app.\\\
\\\
Are您确定要退出?,警告:未提交,MessageBoxButtons.YesNoCancel,MessageBoxIcon.Stop)==的DialogResult。是)
的回报;
,否则
e.Cancel = TRUE;
}
}


I'm trying to include in my Windows Form App a way that when user tries to shutdown windows it opens a dialog box with CANCEL option....that office classic one.

In other topics, people describes how to prevent windows shutdown. They use a dialog box for this. It helps but if the user immediately clicks in any option in this box, windows closes the application.

You can understand what I'm meaning, doing the following test:

In windows Vista or 7 Open Paint, Word or any office soft and begin writing something. Do not save it.

Try to shutdown windows and when the classic save dialog box appears IMMEDIATELY click in "cancel".

You will see that the application continues to work and windows is asking you what you want to do.

I tried to follow this Microsoft Link but if I click "ok" in Message Box, the App closes.

解决方案

   private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (e.CloseReason.Equals(CloseReason.WindowsShutDown))
        {
           if (MessageBox.Show("You are closing this app.\n\nAre you sure you wish to exit ?", "Warning: Not Submitted", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Stop) == DialogResult.Yes)
               return;    
           else    
               e.Cancel = true;
        }
    }

这篇关于阻止Windows关机后取消选项C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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