private void Master_FormClosing(object sender, FormClosingEventArgs e)
        {
            int n = int.Parse(MyClass.Dlookup("select count(*) from  stock where Stock.ISUpload=0"));
            if (n > 0)
            {
                if (MessageBox.Show("数据上传未完成,是否一定要退出程序", "Confirm Message", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    isRunning = false;
                    this.Dispose();
                }
                else
                {
                }
                
            }
            else
            {
                isRunning = false;
                this.Dispose();
            }
        }使用FormClosing事件,无论点击messagebox的确定或者取消都会关闭按钮,这是为什么?

解决方案 »

  1.   

    如果不想退出,需设置e.Cancel=true,private void Master_FormClosing(object sender, FormClosingEventArgs e)
            {
                e.Cancel = false;            int n = int.Parse(MyClass.Dlookup("select count(*) from  stock where Stock.ISUpload=0"));
                if (n > 0)
                {
                    if (MessageBox.Show("数据上传未完成,是否一定要退出程序", "Confirm Message", MessageBoxButtons.OKCancel) == DialogResult.OK)
                    {
                        isRunning = false;
                        this.Dispose();
                    }
                    else
                    {
                        e.Cancel = true;                }
                    
                }
                else
                {
                    isRunning = false;
                    this.Dispose();
                }
            }
      

  2.   

    private void Master_FormClosing(object sender, FormClosingEventArgs e)
            {
                int n = int.Parse(MyClass.Dlookup("select count(*) from  stock where Stock.ISUpload=0"));
                if (n > 0)
                {
                    if (MessageBox.Show("数据上传未完成,是否一定要退出程序", "Confirm Message", MessageBoxButtons.OKCancel) == DialogResult.OK)
                    {
                        isRunning = false;
                        this.Dispose();
                    }
                    else
                    {
                        e.Cancel = true;//取消关闭窗体事件
                    }
                    
                }
                else
                {
                    isRunning = false;
                    this.Dispose();
                }
            }
      

  3.   

    我现在按照你的方法改了,但是menustrip的退出又有问题了这是什么意思?
      

  4.   

    使用FormClosing事件,无论点击messagebox的确定或者取消都会关闭按钮,这是为什么?
    你的关闭按钮是啥意思???
    取消关闭窗体事件是:e.Cancel = true;
      

  5.   

    在closing事件中没必要调用this.Dispose();你去掉试试看,这个错误没有上下文很难判断是什么原因,建议你查看一下抛出异常的堆栈,
     
      

  6.   

    取消关闭窗体事件:
        e.Cancel = true;跟踪代码了看下...