[英]C# Application Restart
I have a log-out option on my WinForms Application that uses this code: 我的WinForms应用程序上有一个使用以下代码的注销选项:
// restart the application once user click on Logout Menu Item
private void eToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Restart();
}
Clicking the Log out option brings up the "are you sure" box with "Yes" or "No" 单击注销选项,弹出“您确定”框,其中显示“是”或“否”
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
//Double check if user wants to exit
var result = MessageBox.Show("Are you sure you want to exit?", "Message",
MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (result == DialogResult.Yes)
{
e.Cancel = false;
}
else
{
e.Cancel = true;
}
}
, yes works no problem, but clicking "No" still restarts the application, how do i fix this? ,是的,没问题,但是单击“否”仍会重新启动应用程序,我该如何解决?
DialogResult confirm = MessageBox.Show("confirm Exit?", "exit", MessageBoxButtons.YesNo);
if (confirm==DialogResult.Yes)
Application.Restart();
else
{
//do some thing
}
Put the dialog box like this inside the MenuItem_Click: 将这样的对话框放入MenuItem_Click中:
// restart the application once user click on Logout Menu Item
private void eToolStripMenuItem_Click(object sender, EventArgs e)
{
//Double check if user wants to exit
var result = MessageBox.Show("Are you sure you want to exit?", "Message",
MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (result == DialogResult.Yes)
{
Application.Restart();
}
}
Leave your FormClosing event empty: 将您的FormClosing事件留空:
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
}
The other way of doing it would be if you absolutely want the dialog box to be implemented in the FormClosing event to override the OnFormClosing()
另一种方法是,如果您绝对希望在FormClosing事件中实现对话框以覆盖
OnFormClosing()
You can do this like this: 您可以这样做:
protected override void OnFormClosing(FormClosingEventArgs e) {
//Double check if user wants to exit
var result = MessageBox.Show("Are you sure you want to exit?", "Message",
MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (result == DialogResult.Yes)
e.Cancel = true;
base.OnFormClosing(e);
}
In this case also the FormClosing
event will remain empty. 在这种情况下,
FormClosing
事件也将保持为空。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.