[英]C# Application Restart
我的WinForms应用程序上有一个使用以下代码的注销选项:
// restart the application once user click on Logout Menu Item
private void eToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Restart();
}
单击注销选项,弹出“您确定”框,其中显示“是”或“否”
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;
}
}
,是的,没问题,但是单击“否”仍会重新启动应用程序,我该如何解决?
DialogResult confirm = MessageBox.Show("confirm Exit?", "exit", MessageBoxButtons.YesNo);
if (confirm==DialogResult.Yes)
Application.Restart();
else
{
//do some thing
}
将这样的对话框放入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();
}
}
将您的FormClosing事件留空:
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
}
另一种方法是,如果您绝对希望在FormClosing事件中实现对话框以覆盖OnFormClosing()
您可以这样做:
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);
}
在这种情况下, FormClosing
事件也将保持为空。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.