private void SellForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("确认要关闭该窗口吗", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
e.Cancel = true;
return;
}
}
private void SellForm_Load(object sender, EventArgs e)
{
try
{
........... //这里面出现bug了
}
catch(Exception ex)
{
MessageBox.Show("数据库访问出错:" + ex.Message);
//FormClosingEventArgs exp -= new FormClosingEventArgs(new CloseReason(),true);
//exp.Cancel = false;
//SellForm_FormClosing sf
this.Close();
}当我的程序跑到catch块里面时,怎么屏蔽SellForm_FormClosing事件。出现问题时,我想让窗口直接关闭,不需要弹出提示框。
closing里面多判断一下这个变量。
this.SellForm_FormClosing-= new FormClosingEventArgs(SellForm_FormClosing);写得可能不对,你在写this.SellForm_FormClosing+=的时候会提示按Tab键自动完成,然后把+号改成-
我可以定义个全局变量closeMessage(bool),然后在catch块中把closeMessage置成FALSE。
然后再在SellForm_FormClosing事件里判断closeMessage是不是TRUE,如果是,就弹出提示框。这样做是不是有点麻烦,而且我不习惯定义这样的全局变量。
this.SellForm_FormClosing+= new FormClosingEventArgs(SellForm_FormClosing);剪切到FormLoad的最后.
就在你说+=时,按tab键不管用,而且FormClosingEventArgs(SellForm_FormClosing)出错,说:FormClosingEventArgs没有采用1个参数。
改成:
this.FormClosing -= SellForm_FormClosing;
this.Close();
private void SellForm_Load(object sender, EventArgs e)
{
try
{
........... //这里面出现bug了
}
catch(Exception ex)
{
MessageBox.Show("数据库访问出错:" + ex.Message);
Application.ExitThread();
this.Close();
}
1楼的建议,好像不好做,因为不管是用户自己点击关闭按钮,还是在程序中调用close方法,都会导致
e.CloseReason ==CloseReason.UserClosing
都怪我,我也没说明白,我的SellForm窗体是从另外一个窗体调用的,如果用Application.ExitThread();会将整个程序退出。这也是我不想要的。当然我也非常感谢10楼的兄台。
9楼的方法我试过了,可以解决问题。但是还有点瑕疵:SellForm窗口一闪而过。不知这个问题可不可以解决。
private void SellForm_FormClosing(object sender, FormClosingEventArgs e)
{
if(!eventStop)
{
if (MessageBox.Show("确认要关闭该窗口吗", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
e.Cancel = true;
return;
}
}
}
private void SellForm_Load(object sender, EventArgs e)
{
try
{
........... //这里面出现bug了
}
catch(Exception ex)
{
eventStop = true;
MessageBox.Show("数据库访问出错:" + ex.Message);
this.Close();
}