【问题标题】:Closing my application is interfering with my Form1_FormClosing event关闭我的应用程序会干扰我的 Form1_FormClosing 事件
【发布时间】:2015-02-18 15:37:12
【问题描述】:

在表单加载时,我试图查看我的应用程序是否可以连接到数据库。如果它没有连接,我想显示一个带有好消息的消息框,然后关闭应用程序。我遇到的问题是,当它尝试关闭应用程序时,它会遇到 Form_closure 事件,询问他们“是否要退出应用程序”,对于无权访问应用程序的用户来说,这看起来有点奇怪/数据库以查看消息框。我只想跳过表单关闭事件并关闭表单,任何帮助将不胜感激。

  private void Form1_Load(object sender, EventArgs e)
  {
     checkcon();
  }
  private void checkcon()
  {
     try
     {
       MSSQL.SqlConnection con = new MSSQL.SqlConnection(constr);
       con.Open();
       con.Close();
     }
     catch (Exception ex)
     {
       MessageBox.Show("Your domain account does not have sufficient privilages to    continue with the application please contact the IS support Team.");              
       Close();
     }

  }

  private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  {
    DialogResult result = MessageBox.Show("Are you sure you want to exit the application?", "Alert", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
     if (result == DialogResult.No)
     {
        e.Cancel = true;
     }
     else
     {
     }
  }

【问题讨论】:

    标签: c# try-catch sqlconnection formclosing


    【解决方案1】:

    添加一个静态布尔变量来检查是否显示对话:

     public static bool HasPermission=true;
     private void checkcon()
      {
         try
         {
           MSSQL.SqlConnection con = new MSSQL.SqlConnection(constr);
           con.Open();
    
           con.Close();
         }
         catch (Exception ex)
         { 
           HasPermission=false;
           MessageBox.Show("Your domain account does not have sufficient privilages to    continue with the application please contact the IS support Team.");              
           Close();
         }
    
      }
    
     private void Form1_FormClosing(object sender, FormClosingEventArgs e)
      { if (HasPermission)
        {
        DialogResult result = MessageBox.Show("Are you sure you want to exit the application?", "Alert", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
         if (result == DialogResult.No)
         {
            e.Cancel = true;
         }
         else
         {
         }
         }
      }
    

    【讨论】:

    • 欣赏它,所以你基本上将静态布尔声明为真,如果它没有找到连接则为假,然后它会点击表单关闭并且该事件说如果它是真的而不是运行该事件关闭是我的理解正确吗???
    【解决方案2】:

    我认为这很容易。

    只需创建一个布尔类型的变量并检查用户是否可以访问数据库?如果没有,请在不确认的情况下关闭它。

    让我通过修改你的代码来解释一下:

      bool userGotPermissionToYourApplication = true;
    
      private void Form1_Load(object sender, EventArgs e)
      {
         checkcon();
      }
      private void checkcon()
      {
         try
         {
           MSSQL.SqlConnection con = new MSSQL.SqlConnection(constr);
           con.Open();
           con.Close();
    
         }
         catch (Exception ex)
         {
           MessageBox.Show("Your domain account does not have sufficient privilages to    continue with the application please contact the IS support Team.");              
           userGotPermissionToYourApplication = false;
           Close();
         }
    
      }
    
      private void Form1_FormClosing(object sender, FormClosingEventArgs e)
      {
        if(userGotPermissionToYourApplication)
        {
            DialogResult result = MessageBox.Show("Are you sure you want to exit the application?", "Alert", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (result == DialogResult.No)
            {
                e.Cancel = true;
            }
            else
            {
            }
        }
    }
    

    【讨论】:

    • 谢谢 Vishal 但是 apomene 打败了你,你的解决方案完全一样,我理解这个概念。
    猜你喜欢
    • 1970-01-01
    • 2021-01-06
    • 2021-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-23
    相关资源
    最近更新 更多