消息框在C#窗口应用程序中的FormClosing事件中出现两次? [英] Message Box appears twice in FormClosing Event in C# Window Application?

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

问题描述

当用户关闭表单窗口时,将出现警告消息框。如果我单击是或否按钮再次出现消息框而不是关闭窗体。第二次,如果我单击是或否按钮,它将根据选择完美执行。



如何解决这个问题?



  if (e.CloseReason == CloseReason.UserClosing)
{
if (bIsButtonClicked == true
{
if (_threadProcessCCTV.IsAlive)
{
bThreadToBeSuspendFlag = true ;
_AutoResetEvent.Reset();
动态 mboxResult = MessageBox.Show( 您确定要退出表单吗? Tranxit CCTV Job Creation Tool,MessageBoxButtons.YesNo);
if (mboxResult == DialogResult.No)
{
_AutoResetEvent.Set();
bThreadToBeSuspendFlag = false ;
/ * 从关闭表单中取消Closing事件。 * /
e.Cancel = true ;
}

else if (mboxResult == DialogResult.Yes )
{
_LogWriter.LogMessage( 用户已停止进程。 true );
_LogWriter.LogMessage( ***************** ************************************************** ******************** true );
/ * 关闭表单。 * /
e.Cancel = false ;
Application.Exit();
}
}
}
}

解决方案

在Form.Designer.cs中,下面已经初始化了两次。删除后。它工作正常。



 这个 .FormClosing + =  new  System.Windows.Forms.FormClosingEventHandler( this  .frmTranxitCCTVJobCreation_FormClosing); 


我刚拿出特定于您的应用程序的赔率:

  private   void  frmMain_FormClosing( object  sender,FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
dynamic mboxResult = MessageBox.Show( 您确定要退出表单吗? Tranxit CCTV工作创建工具,MessageBoxButt ons.YesNo);
if (mboxResult == DialogResult.No)
{
/ * 从关闭表单中取消Closing事件。 * /
e.Cancel = true ;
}

else if (mboxResult == DialogResult.Yes )
{
/ * 关闭表单。 * /
e.Cancel = false ;
Application.Exit();
}
}
}

当我点击时,我收到消息框。

否取消收盘,没有更多消息。

是关闭表单并关闭应用程序。



所以我认为你需要看看你剩下的代码...


When user closing Form Window, Warning Message Box will appear. If i click Yes or No button again appear the Message Box instead of closing Form. Second time if i click Yes or No button it will execute perfectly according to selection.

How to solve this?

if (e.CloseReason == CloseReason.UserClosing)
                {
                    if (bIsButtonClicked == true)
                    {
                        if (_threadProcessCCTV.IsAlive)
                        {
                            bThreadToBeSuspendFlag = true;
                            _AutoResetEvent.Reset();
                            dynamic mboxResult = MessageBox.Show("Are you sure you want to exit the form?", "Tranxit CCTV Job Creation Tool", MessageBoxButtons.YesNo);
                            if (mboxResult == DialogResult.No)
                            {
                                _AutoResetEvent.Set();
                                bThreadToBeSuspendFlag = false;
                                /* Cancel the Closing event from closing the form. */
                                e.Cancel = true;
                            }

                            else if (mboxResult == DialogResult.Yes)
                            {
                                _LogWriter.LogMessage("Process has been stopped by user.", true);
                                _LogWriter.LogMessage("***************************************************************************************", true);
                                /* Closing the form. */
                                e.Cancel = false;
                                Application.Exit();
                            }
                        }
                    }
                }

解决方案

In Form.Designer.cs, Below has initialized two times. After removing them. It's working fine.

this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.frmTranxitCCTVJobCreation_FormClosing);


I just took the odds'n'ends that are specific to your app out:

private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
    {
    if (e.CloseReason == CloseReason.UserClosing)
        {
        dynamic mboxResult = MessageBox.Show("Are you sure you want to exit the form?", "Tranxit CCTV Job Creation Tool", MessageBoxButtons.YesNo);
        if (mboxResult == DialogResult.No)
            {
            /* Cancel the Closing event from closing the form. */
            e.Cancel = true;
            }

        else if (mboxResult == DialogResult.Yes)
            {
            /* Closing the form. */
            e.Cancel = false;
            Application.Exit();
            }
        }
    }

And when I click, I get the message box.
"No" cancels the close, with no more messages.
"Yes" closes the form and shuts the app.

So I think you need to look at the rest of your code...


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

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