在表单加载时,我试图查看我的应用程序是否可以连接到数据库。如果它没有连接,我想显示一个带有好消息的消息框,然后关闭应用程序。我遇到的问题是,当它尝试关闭应用程序时,它会遇到 Form_looking 事件,询问他们“是否要退出应用程序”,对于无权访问应用程序的用户来说,这看起来有点奇怪/数据库来查看消息框。我只想跳过表单关闭事件并关闭表单,任何帮助将不胜感激。
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
{
}
}
最佳答案
添加一个静态 bool 变量作为检查是否显示对话:
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
{
}
}
}
关于c# - 关闭我的应用程序会干扰我的 Form1_FormClosing 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28587462/