读书人

vs2008点击窗体右上角的红叉退出的有关

发布时间: 2012-05-12 15:39:31 作者: rapoo

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();
}
}

读书人网 >C#

热点推荐