日期:2014-05-18  浏览次数:20989 次

vs2008点击窗体右上角的红叉退出的问题
C# code
 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的确定或者取消都会关闭按钮,这是为什么?

------解决方案--------------------
如果不想退出,需设置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();
}
}
------解决方案--------------------
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();
}
}