[英]I have to click multiple times before the dialog box closes, or performs the next action…How do I fix this?
Here is my C# code:这是我的 C# 代码:
private void StudentReg_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult dialog = MessageBox.Show("Do you really want to close this window?", "Exit", MessageBoxButtons.YesNo);
if (dialog == DialogResult.Yes)
{
Application.Exit();
}
else if (dialog == DialogResult.No)
{
e.Cancel = true;
}
}
I doubt if you really want Application.Exit();
我怀疑你是否真的想要
Application.Exit();
: you prompt (bold is mine) :你提示(粗体是我的)
Do you really want to close this window
你真的要关闭这个window
It is the window ( Form
), not the entire Aplication
you want to close.它是window (
Form
),而不是您要关闭的整个Aplication
程序。 If it's your case, you can just do nothing and let the form (window) close itself:如果是您的情况,您可以什么都不做,让表单(窗口)自行关闭:
private void StudentReg_FormClosing(object sender, FormClosingEventArgs e)
{
// Cancel closing (i.e. do NOT close the form) if and only if user's chosen "No".
// Do nothing (i.e. do not cancel) in other cases (let the form be closed)
// So we assign true (cancel) if "No" has been pressed, false otherwise
e.Cancel = MessageBox.Show(
"Do you really want to close this window?",
"Exit",
MessageBoxButtons.YesNo) == DialogResult.No;
}
Depending on how you setup your application like if this is your main form or you have multiple you may need to just remove the event to avoid duplicate calls to it.根据您设置应用程序的方式,例如这是您的主表单还是您有多个,您可能只需要删除该事件以避免重复调用它。
Remove the event handler before calling application.exit and I think your issue will be solved.在调用 application.exit 之前删除事件处理程序,我认为您的问题将得到解决。
This is psuedo-code.这是伪代码。
private void StudentReg_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult dialog = MessageBox.Show("Do you really want to close this window?", "Exit", MessageBoxButtons.YesNo);
if(dialog == DialogResult.Yes)
{
StudentReg.FormClosing -= StudentReg_FormClosing;
Application.Exit();
}
else if(dialog == DialogResult.No)
{
e.Cancel = true;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.