winform 中点击"X"弹出提示是否关闭的问题...........

fwqkey 2009-07-08 10:41:14

我下面的代码中在弹出的提示窗口中点击是的话该窗口会再弹出一次.再点击一次才退出程序
好像是Application.Exit()这句代码又重新触发了事件.
如果不用这句代码的话则只能关闭本窗口,不能退出整个应用程序..
请问有什么好的解决办法???

//点击关闭
private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult re = MessageBox.Show("确定要退出吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (re == DialogResult.Yes)
{
e.Cancel = false;
Application.Exit();
}
else
{
e.Cancel = true;
}
}

...全文
813 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
小艳 2012-03-08
  • 打赏
  • 举报
回复
huo qu keyongfen
淘尼 2011-12-02
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 h_w_king 的回复:]
if (e.CloseReason != CloseReason.ApplicationExitCall)
{
DialogResult re = MessageBox.Show("确定要退出吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (re == DialogResult.Yes)
……
[/Quote]
多谢,帮我解决问题了~~
surlew 2009-07-08
  • 打赏
  • 举报
回复

private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("确认要关闭软件? ", "关闭 ", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
{

}
else
{
e.Cancel = true;
}
}


上面就可以了
hanhanmj 2009-07-08
  • 打赏
  • 举报
回复
加个 变量记录是否关闭吧..


private bool isExit = false; //是否确定退出

private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
if (!isExit)
{
DialogResult re = MessageBox.Show("确定要退出吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (re == DialogResult.Yes)
{
e.Cancel = false;
isExit = true;
Application.Exit();
}
else
{
e.Cancel = true;
}
}
} //这样应该可以..
fwqkey 2009-07-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 h_w_king 的回复:]
if (e.CloseReason != CloseReason.ApplicationExitCall)
            {
                DialogResult re = MessageBox.Show("确定要退出吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (re == DialogResult.Yes)
                {
                    e.Cancel = false;

                    Application.Exit();
                }
                else
                {
                    e.Cancel = true;
                }
            }
[/Quote]

正解..结贴给分..谢谢
yulitian 2009-07-08
  • 打赏
  • 举报
回复
FormClosingEventArgs e

e啊,e!

e有个属性,关闭原因,cloaereson
是个枚举

判断一下,是用户关闭还是应用程序退出就行了
是UserClose,就出现你的提示逻辑,否则什么也不做
h_w_king 2009-07-08
  • 打赏
  • 举报
回复

if (e.CloseReason != CloseReason.ApplicationExitCall)
{
DialogResult re = MessageBox.Show("确定要退出吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (re == DialogResult.Yes)
{
e.Cancel = false;

Application.Exit();
}
else
{
e.Cancel = true;
}
}
fwqkey 2009-07-08
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 lianshaohua 的回复:]
        private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult re = MessageBox.Show("确定要退出吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
            if (re == DialogResult.Yes)
            {
                e.Cancel = false;
            }
            else
            {
                e.Cancel = true;
            }
        }

[/Quote]


倒,这个这能关闭当前窗口.不能关闭整个应用程序,这个可以的话我还来这问?
zenowolf 2009-07-08
  • 打赏
  • 举报
回复
private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult re = MessageBox.Show("确定要退出吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (re == DialogResult.Yes)
{
e.Cancel = false;
}
else
{
e.Cancel = true;
}
}
ztenv 2009-07-08
  • 打赏
  • 举报
回复
private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult re = MessageBox.Show("确定要退出吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (re == DialogResult.Yes)
{
e.Cancel = false;
}
else
{
e.Cancel = true;
}
}

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧