C# 应用程序重启
C# Application Restart
我的 WinForms 应用程序上有一个使用此代码的注销选项:
// restart the application once user click on Logout Menu Item
private void eToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Restart();
}
单击注销选项会弹出带有 "Yes" 或 "No"
的 "are you sure" 框
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;
}
}
,是的,没问题,但是单击 "No" 仍然会重新启动应用程序,我该如何解决这个问题?
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
事件也将保持为空。
我的 WinForms 应用程序上有一个使用此代码的注销选项:
// restart the application once user click on Logout Menu Item
private void eToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Restart();
}
单击注销选项会弹出带有 "Yes" 或 "No"
的 "are you sure" 框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;
}
}
,是的,没问题,但是单击 "No" 仍然会重新启动应用程序,我该如何解决这个问题?
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
事件也将保持为空。