如何在单击消息框确定后关闭窗体?

时间:2015-03-27 20:30:30

标签: c# winforms

程序有两种形式:登录后登录和主表单。 启动程序后,它连接到数据库并检查是否有新版本可用,如果有,它会立即显示一个MessageBox,让用户知道下载新版本。

当用户单击“确定”时,需要关闭应用程序,以便用户在下载新版本之前无法再使用它。问题是,点击好后,仍会显示登录表单。我的课程代码如下:

 DialogResult dialog = MessageBox.Show("FleetTrack™ update required.\n\nA new version of FleetTrack™ is available on your Driver Hub. You must download"
        + " the latest update to use FleetTrack™.", "FleetTrack™ Update Required", MessageBoxButtons.OK);
        if (dialog == DialogResult.OK)
        {


            Application.ExitThread();
        }

不太确定我需要做什么。如果运行的版本与数据库中显示的版本不同,应用程序会成功显示弹出窗口,但单击“确定”后,它只会正常加载登录表单。

1 个答案:

答案 0 :(得分:5)

使用Application.Exit()而不是Application.ExitThread()

如果您在Application.Run()之前显示对话框,那么您需要确保的是,如果需要更新版本,则不要调用Application.Run()。

if (updateRequired)
{
    DialogResult dialog = MessageBox.Show("FleetTrack™ update required.\n\nA new version of FleetTrack™ is available on your Driver Hub. You must download"
+ " the latest update to use FleetTrack™.", "FleetTrack™ Update Required", MessageBoxButtons.OK);
    if (dialog == DialogResult.OK)
    {
        Application.Exit();
    }
} else 
    Application.Run(new Login());

updateRequired是您维护的布尔值,用于检查是否需要更新应用程序。

相关问题