C#Closing事件为何点击退出按钮不能关闭

vc354155474 2012-03-24 11:27:54
C#Closing事件为何点击退出按钮不能关闭,我要的是那种点击退出弹出是否退出,现在点击退出不能退出。如果直接点击右上角“X”可以直接退出 而先点击退出在点击“X”才会弹出大家帮帮忙
private void button5_Click(object sender, EventArgs e)
{
this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);


}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("是否要退出", "Application", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No)
{
e.Cancel = true;
}

}
...全文
446 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
嘴哥臭鼬 2012-03-26
  • 打赏
  • 举报
回复
public Form1()
{
InitializeComponent();
this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("是否要退出", "Application", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No)
e.Cancel = true;
else
base.Close();

}
mngzilin 2012-03-26
  • 打赏
  • 举报
回复
public Form1()
{
InitializeComponent();
this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("是否要退出", "Application", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No)
e.Cancel = true;

}
caiqimin__ 2012-03-26
  • 打赏
  • 举报
回复
楼主,这是我修改的程序,看看满意不
public Form1()
{
InitializeComponent();
this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
}
bool bClicked = false;
private void button1_Click(object sender, EventArgs e)
{
bClicked = true;
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (!bClicked)
{
e.Cancel = false;
return;
}
if (MessageBox.Show("是否要退出", "Application", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No)
{
e.Cancel = true;
}

}
wy811007 2012-03-26
  • 打赏
  • 举报
回复
2L正解 LZ只写了否按钮 没写是按钮
而且还写倒了 o(︶︿︶)o 唉
ycproc 2012-03-25
  • 打赏
  • 举报
回复

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("是否要退出", "Application", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No)
{
e.Cancel = true;
}

}


你让别人不确定退出 然后给别人退出?
devmiao 2012-03-25
  • 打赏
  • 举报
回复
private void button5_Click(object sender, EventArgs e)
{
this.Close();


}
cnwin 2012-03-25
  • 打赏
  • 举报
回复
就是你的退出按钮里没有关闭窗口的代码。
看楼主的代码,似乎对程序理解有误。你的关闭按钮里注册的事件,也就是只有点了关闭按钮才会引发询问。那楼主是想点叉号不询问吗?楼主的意图最好表述清楚些。
xxm30 2012-03-25
  • 打赏
  • 举报
回复
1楼正确。楼主缺少了退出部分,只有取消!
  • 打赏
  • 举报
回复
2楼正解[Quote=引用 2 楼 devmiao 的回复:]
private void button5_Click(object sender, EventArgs e)
{
this.Close();


}
[/Quote]
EnForGrass 2012-03-24
  • 打赏
  • 举报
回复

if (MessageBox.Show("您确认要退出吗?", "退出系统", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
this.Dispose();
// Application.Exit();
}
else
e.Cancel = true;

111,126

社区成员

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

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

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