C# 为什么Application.Exit会提示我两次?
当我按下表单上的X时,如何阻止消息框显示两次 ? 仅供参考butoon的咔哒声很好,是X提示了我两次C# 为什么Application.Exit会提示我两次?,c#,winforms,C#,Winforms,当我按下表单上的X时,如何阻止消息框显示两次 ? 仅供参考butoon的咔哒声很好,是X提示了我两次 private void xGameForm_FormClosing(object sender, FormClosingEventArgs e) { //Yes or no message box to exit the application DialogResult Response; Response = MessageBox.Sho
private void xGameForm_FormClosing(object sender, FormClosingEventArgs e)
{
//Yes or no message box to exit the application
DialogResult Response;
Response = MessageBox.Show("Are you sure you want to Exit?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
if (Response == DialogResult.Yes)
Application.Exit();
}
public void button1_Click(object sender, EventArgs e)
{
Application.Exit();
}
在您发出应用程序退出调用时,表单仍处于打开状态,关闭事件甚至尚未完成处理。退出调用导致窗体关闭。由于表单尚未关闭,它将再次通过关闭路径并点击事件处理程序
解决这个问题的一种方法是在实例变量中记住决策
private bool m_isExiting;
private void xGameForm_FormClosing(object sender, FormClosingEventArgs e)
{
if ( !m_isExiting ) {
//Yes or no message box to exit the application
DialogResult Response;
Response = MessageBox.Show("Are you sure you want to Exit?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
if (Response == DialogResult.Yes) {
m_isExiting = true;
Application.Exit();
}
}
public void button1_Click(object sender, EventArgs e)
{
Application.Exit();
}
在您发出应用程序退出调用时,表单仍处于打开状态,关闭事件甚至尚未完成处理。退出调用导致窗体关闭。由于表单尚未关闭,它将再次通过关闭路径并点击事件处理程序
解决这个问题的一种方法是在实例变量中记住决策
private bool m_isExiting;
private void xGameForm_FormClosing(object sender, FormClosingEventArgs e)
{
if ( !m_isExiting ) {
//Yes or no message box to exit the application
DialogResult Response;
Response = MessageBox.Show("Are you sure you want to Exit?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
if (Response == DialogResult.Yes) {
m_isExiting = true;
Application.Exit();
}
}
public void button1_Click(object sender, EventArgs e)
{
Application.Exit();
}
您尝试退出应用程序两次。请尝试以下操作:
private void xGameForm_FormClosing(object sender, FormClosingEventArgs e) {
if (DialogResult.No == MessageBox.Show("Are you sure you want to exit?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2))
e.Cancel = true;
}
private void button1_Clink(object sender, EventArgs e) {
Close();
}
您尝试退出应用程序两次。请尝试以下操作:
private void xGameForm_FormClosing(object sender, FormClosingEventArgs e) {
if (DialogResult.No == MessageBox.Show("Are you sure you want to exit?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2))
e.Cancel = true;
}
private void button1_Clink(object sender, EventArgs e) {
Close();
}
这将有助于:
ExitThread 这将起作用:
ExitThread 准确地说,这正是我要说的。MS很高兴您取消申请退出或任何表单。请关闭,但您必须以这种方式执行!准确地说,这正是我要说的。MS很高兴您取消申请退出或任何表单。请关闭,但您必须以这种方式执行!如果用户确实单击了“否”,那么取消退出不是更简单吗?否则,用户确认他希望退出,那么就没有更多的事情要做了,正如我所看到的,因为应用程序已经关闭了。此外,还有一个可读性优势。与提出的解决方案相比,更明显的是看到用户的意图。虽然没有伤害的意思,但我只分享我的想法=如果用户确实单击了“否”,那么取消退出不是更简单吗?否则,用户确认他希望退出,那么就没有更多的事情要做了,正如我所看到的,因为应用程序已经关闭了。此外,还有一个可读性优势。与提出的解决方案相比,更明显的是看到用户的意图。虽然没有伤害的意思,但我只分享我的想法=