【问题标题】:Closing form and capture closing event关闭表单并捕获关闭事件
【发布时间】:2013-03-04 06:17:03
【问题描述】:

每个人。 我遇到过这样的问题。在我的应用程序中,我通过 ShowDialog 方法从我的主表单中显示第二个表单。在这种形式中,我有一些文本框可以连接到数据库和一个连接按钮。如果用户单击 X,则应用程序退出。但是如果用户单击“连接” - 我连接到数据库并关闭我的第二个表单。为了捕捉关闭事件,我使用 FormClosing 方法,应用程序询问我是否要关闭应用程序,如果是,则退出。问题是当我单击按钮时,FormClosing 事件会触发并询问我是否要退出。如何避免?我尝试使用发件人,但它不起作用。

这是我的代码:

private void Connect_Click(object sender, EventArgs e)
{
    try
    {
         orcl.connect(userID.Text, Password.Text, comboTNS.Text);

         if (orcl.ifHasRows("select dbclass from setupdbversion where dbclass='SECURITY' and rownum=1"))
         {//my stuff
             this.Close();
         }

     }
     catch (Exception ex)
     {
          MessageBox.Show(ex.Message.ToString());
     };
}


private void SecConnForm_FormClosing_1(object sender, FormClosingEventArgs e)
{   
     MessageBox.Show(sender.ToString());
     if (e.CloseReason == CloseReason.UserClosing)
     {
          MessageBox.Show(sender.ToString());
          if (string.Equals((sender as Form).Name, @"SecConnForm")) //it doesn't work as in any cases the sender is my form, not a button (when i click on button of course)
          {
               if (MessageBox.Show(this, "Really exit?", "Closing...",
                    MessageBoxButtons.OKCancel, MessageBoxIcon.Question)
                       == DialogResult.Cancel) 
                   e.Cancel = true;
               else
                   Application.Exit();

          }
          else
          {
               //other stuff goes..
          }
     }
}

【问题讨论】:

  • “当我点击按钮时”,请详细说明您说的是哪个按钮?

标签: c# forms sender


【解决方案1】:

无论是通过代码还是用户点击,每次关闭表单时都会触发表单关闭事件。

你需要的是与此类似的东西。

private boolean bFormCloseFlag = false;

private void Connect_Click(object sender, EventArgs e)
{
    try
    {
         orcl.connect(userID.Text, Password.Text, comboTNS.Text);

         if (orcl.ifHasRows("select dbclass from setupdbversion where dbclass='SECURITY' and rownum=1"))
         {//my stuff
             bFormCloseFlag = true;
             this.Close();
         }

     }
     catch (Exception ex)
     {
          MessageBox.Show(ex.Message.ToString());
     };
}


private void SecConnForm_FormClosing_1(object sender, FormClosingEventArgs e)
{   
     if (bFormCloseFlag = false)
     {
        MessageBox.Show(sender.ToString());
        if (e.CloseReason == CloseReason.UserClosing)
        {
          MessageBox.Show(sender.ToString());
          if (string.Equals((sender as Form).Name, @"SecConnForm")) //it doesn't work as in any cases the      sender is my form, not a button (when i click on button of course)
          {
               if (MessageBox.Show(this, "Really exit?", "Closing...",
                    MessageBoxButtons.OKCancel, MessageBoxIcon.Question)
                       == DialogResult.Cancel) 
                   e.Cancel = true;
               else
                   Application.Exit();

          }
          else
          {
               //other stuff goes..
          }
       }
     }
}

此标志将简单地检查表单是通过单击“X”按钮关闭还是由您的代码关闭。

【讨论】:

    【解决方案2】:

    this.Close() 触发“UserClosing”的关闭类型 可能只是隐藏对话框而不是 this.close()?

    【讨论】:

    • 无论如何你都可以添加评论来传达你的信息:-)
    猜你喜欢
    • 2010-11-25
    • 1970-01-01
    • 1970-01-01
    • 2013-01-30
    • 2020-03-10
    • 2015-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多