2

At the form load i am trying to see if my application can connect to the database or not. If it doesn't connect i want to show a message box with a nice message, and close the application. The problem that i am having is that when the it trys to close the application it hits the Form_closing event that asks them "if they want to exit the application" it looks kind of strange for a user that does not have access to the application/database to view the message box. I just want to skip the form closing event and just close the form, any help would be greatly appreciated.

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

2 回答 2

2

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

 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
     {
     }
     }
  }
于 2015-02-18T15:43:17.400 回答
0

I think that is very much easy.

Just create a variable of type boolean and check if user has access to database? If not then please close it without any confirmation.

Let me explain you by modifying your code:

  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
        {
        }
    }
}
于 2015-02-18T15:46:12.463 回答