C#应用程序重启

时间:2016-03-31 05:04:42

标签: c# winforms logout

我的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;
        }
    }

,是没有问题,但点击“否”仍然重启应用程序,我该如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

cars.map({ car=> tf.go( car)}) // it's what I want to do

// other.scala - tf class
import packname.VH // not work
class TF ... {
 def go( car: car) // not work
 def go( car: VH.car) // not work
}

答案 1 :(得分:2)

将这样的对话框放在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事件也将保持为空。