按照这里的建议在项目的 Main 方法中创建我的登录表单,如下所示:
[MTAThread]
static void Main()
{
AppDomain.CurrentDomain.UnhandledException += Unhandled;
frmLogin loginForm = new frmLogin();
if (loginForm.ShowDialog() != DialogResult.OK)
{
// If they hit "Close" just use the default values for now (for testing)
HHSConsts.userName = "duckbilled";
HHSConsts.pwd = "platypus";
HHSConsts.currentSiteNum = "Packers20Seahawks19";
}
else
{
HHSConsts.userName = loginForm.UserName;
HHSConsts.pwd = loginForm.Password;
HHSConsts.currentSiteNum = loginForm.SiteNumber;
}
loginForm.Dispose();
Application.Run(new frmMain());
}
登录表单有两个按钮,“确定”和“关闭”:
private void buttonOK_Click(object sender, EventArgs e)
{
HHSConsts.userName = textBoxUsername.Text.Trim();
HHSConsts.pwd = textBoxPwd.Text.Trim();
HHSConsts.currentSiteNum = listBoxSitesWithFetchedData.SelectedItem.ToString();
// TODO: Prevent shutdown if "OK" is selected and there are any missing or bogus values?
this.Close();
}
private void buttonClose_Click(object sender, EventArgs e)
{
this.Close();
}
这很公平,但是在登录表单关闭和主表单显示之间存在一定的延迟。有没有办法缩小这个差距,使间隔不那么明显?
更新
替换这个:
Application.Run(new frmMain());
...有了这个:
Application.Run(new Form());
...在 Program.cs 中导致以下结果:
单击登录表单上的确定按钮:应用程序关闭(主表单从不显示)
单击登录表单上的关闭按钮:登录表单上的所有控件都消失了,但表单保持不变......?!?