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事件。出现问题时,我想让窗口直接关闭,不需要弹出提示框。

解决方案 »

  1.   

    在FormClosing 判断 e.CloseReason 有很多你自己看下
      

  2.   

    最简单的办法就是写多一个变量。
    closing里面多判断一下这个变量。
      

  3.   


    this.SellForm_FormClosing-= new FormClosingEventArgs(SellForm_FormClosing);写得可能不对,你在写this.SellForm_FormClosing+=的时候会提示按Tab键自动完成,然后把+号改成-
      

  4.   

    MessageBox.Show("数据库访问出错:" + ex.Message);这句删掉不就得了.
      

  5.   

    这样倒是可以实现。
    我可以定义个全局变量closeMessage(bool),然后在catch块中把closeMessage置成FALSE。
    然后再在SellForm_FormClosing事件里判断closeMessage是不是TRUE,如果是,就弹出提示框。这样做是不是有点麻烦,而且我不习惯定义这样的全局变量。
      

  6.   

    在FormLoad事件最后声明FormClosing事件,就是将.Designer.cs里面
    this.SellForm_FormClosing+= new FormClosingEventArgs(SellForm_FormClosing);剪切到FormLoad的最后.
      

  7.   


    就在你说+=时,按tab键不管用,而且FormClosingEventArgs(SellForm_FormClosing)出错,说:FormClosingEventArgs没有采用1个参数。
      

  8.   

    第二种方法是:在catch中减去close事件
      

  9.   

    this.Close();
    改成:
    this.FormClosing -= SellForm_FormClosing;
    this.Close();
      

  10.   


            private void SellForm_Load(object sender, EventArgs e)
            {
                try
                {
                    ...........  //这里面出现bug了
                }
                catch(Exception ex)
                {
                    MessageBox.Show("数据库访问出错:" + ex.Message);
                     Application.ExitThread();
                    this.Close();
                }
      

  11.   

    9楼和10楼的方法都是可以的。
    1楼的建议,好像不好做,因为不管是用户自己点击关闭按钮,还是在程序中调用close方法,都会导致
    e.CloseReason ==CloseReason.UserClosing
      

  12.   

    看来我回复的晚了.
    都怪我,我也没说明白,我的SellForm窗体是从另外一个窗体调用的,如果用Application.ExitThread();会将整个程序退出。这也是我不想要的。当然我也非常感谢10楼的兄台。
    9楼的方法我试过了,可以解决问题。但是还有点瑕疵:SellForm窗口一闪而过。不知这个问题可不可以解决。
      

  13.   

    设置一个变量控制一下。private bool eventStop;
    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();
                }