关闭我的应用程序会干扰我的 Form1_FormClosing 事件

Closing my application is interfering with my Form1_FormClosing event

在表单加载时,我试图查看我的应用程序是否可以连接到数据库。如果它没有连接,我想显示一个带有好消息的消息框,然后关闭应用程序。我遇到的问题是,当它尝试关闭应用程序时,它会遇到询问他们 "if they want to exit the application" 的 Form_closing 事件,对于无法访问 [=] 的用户来说,这看起来有点奇怪15=] 查看消息框。我只想跳过表单关闭事件并关闭表单,我们将不胜感激。

  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
     {
     }
  }

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

 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
     {
     }
     }
  }

我觉得这很容易。

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

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

  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
        {
        }
    }
}