[英]C# Dialog keeps pop up when click yes in Form_FormClosing
Adding an event in my C# winForms the FormClosing
but the dialog keeps pop-up after adding the code Close()
在我的 C# winForms 中添加一个事件
FormClosing
但添加代码Close()
后对话框不断弹出
private void AdminPanel_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult result = (MessageBox.Show(this,
"Are you sure you want to close the Application?",
"Exit",
MessageBoxButtons.YesNo, MessageBoxIcon.Question));
if (result == DialogResult.Yes)
{
//dialog keeps poping up when i try to close the form
Close();
}
else {
e.Cancel = true;
}
}
Have you tried removing the Close();
您是否尝试过删除
Close();
call?称呼? You're already in a
Closing
event handler – unless you cancel the close, it's going to happen anyway.您已经在
Closing
事件处理程序中——除非您取消关闭,否则无论如何它都会发生。
You don't need to add a call to Close
method inside FormClosing
because you are already in the form close call pattern of Application.您不需要在
FormClosing
中添加对Close
方法的调用,因为您已经处于 Application 的表单关闭调用模式中。
You need to write:你需要写:
private void AdminPanel_FormClosing(object sender, FormClosingEventArgs e)
{
var result = MessageBox.Show(this,
"Are you sure you want to close the Application?",
"Exit",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) );
if ( result != DialogResult.Yes )
{
e.Cancel = true;
}
}
If DialogResult.Yes
then it does nothing to e.Cancel
and the form is closed and the FormClosed
event is called.如果
DialogResult.Yes
那么它对e.Cancel
没有任何作用并且表单被关闭并且FormClosed
事件被调用。
If e.Cancel
is set to true
then the Close
call is cancelled by the Application manager and FormClosed
is not called.如果
e.Cancel
设置为true
,则应用程序管理器会取消Close
调用,并且不会调用FormClosed
。
If you add a call to Close
in FormClosing
you will get a stack overflow: the box is shown and shown and shown and shown and shown and so on without end as long as you click on Yes
, unless you click on No
...如果您在
FormClosing
中添加对Close
的调用,则会出现堆栈溢出:只要您单击Yes
,该框就会显示并显示并显示并显示和显示等等,除非您单击No
...
If you want to force the application ending you can use in the FormClosed
event:如果您想强制应用程序结束,您可以在
FormClosed
事件中使用:
Environment.Exit(0);
But perhaps you have something somewhere that prevents the close.但也许你有什么东西可以阻止关闭。
Do you have code in FormClosed
?您在
FormClosed
中有代码吗? Does it throw an exception?它会抛出异常吗? Else you need to catch it because it stops the close.
否则你需要抓住它,因为它会停止关闭。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.