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

Jus*_*tin 3 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();
        }
Run Code Online (Sandbox Code Playgroud)

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

Pra*_*ose 6

使用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());
Run Code Online (Sandbox Code Playgroud)

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