C#窗口应用程序中的FormClosing事件? [英] FormClosing Event in C# Window Application?

查看:178
本文介绍了C#窗口应用程序中的FormClosing事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用线程构建了一个小型Win Form应用程序。在用户单击表单上的关闭按钮处理应用程序时,我必须显示警告消息框。



我写了下面的代码,工作正常。但问题是消息框出现两次。如何克服这个问题。



I have build one small Win Form App with Threading. While processing the application if user clicked Close button on the Form then i must show warning Message box.

I have written below code and it is working fine. But problem is "Message box appears twice". How to overcome this.

private void frmTranxitCCTVJobCreation_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason != CloseReason.UserClosing)
                return;
            if (bIsButtonClicked == true)
            {
                if (_threadProcessCCTV.IsAlive)
                {
                    if (MessageBox.Show("Are you sure you want to close the form?", "Warning", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                        e.Cancel = false;
                    else
                        // Cancel the Closing event from closing the form.
                        e.Cancel = true;
                }
            }
        }

推荐答案

试试这个



try this

if(e.CloseReason == CloseReason.UserClosing)
            {
                dynamic result = MessageBox.Show("Do You Want To Exit?", "Application Name", MessageBoxButtons.YesNo );
                if (result == DialogResult.Yes )
                {
                    Application.Exit();
                }
              
                if (result == DialogResult.No)
                {
                    e.Cancel = true;
                }
            }


这篇关于C#窗口应用程序中的FormClosing事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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