我的 c# windows 应用程序中有一个登录表单和 MDI 主表单。我在 MDI 表单加载事件中像这样打开我的登录表单。当登录成功时,只有它退出并启用 MDI 主窗体。最近只有我发现,如果我关闭我的登录表单,它就会关闭,然后它会毫无障碍地启用我的 MDI 主程序。
这就是我在 MDI 主窗体中加载登录名的方式。
private void MDiMain_Load(object sender, EventArgs e)
{
setDisplaysize();
Form newLogin = new FormControllers.FrmLogin();
newLogin.StartPosition = FormStartPosition.CenterScreen;
//newLogin.Show(this);
newLogin.ShowDialog(this);
newLogin.Focus();
newLogin.TopMost = true;
newLogin.Activate();
}
然后我尝试使用此代码段更改我的应用程序
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
FormControllers.FrmLogin fLogin = new FormControllers.FrmLogin();
if (fLogin.ShowDialog() == DialogResult.OK)
{
Application.Run(new MDiMain());
}
else
{
Application.Exit();
}
}
现在表单登录打开,但成功登录后,MDI 主表单没有启动。我在这里做错了什么?
此外,这是我在登录表单中的登录按钮代码
private void btnLogin_Click(object sender, EventArgs e)
{
string txtPass = "";
string txttPassword = "";
string txtHoldStr = "";
String txtStringst1 = "";
char chrFstep ='c';
char chrSstep ='c';
int testInt = 0;
using (DataControllers.RIT_Allocation_Entities EntityModel = new DataControllers.RIT_Allocation_Entities())
{
try
{
userHeadModel = EntityModel.TBLU_USERHED.Where(x => x.USERHED_USERCODE == (txtUserName.Text.Trim())).FirstOrDefault();
txtPass = userHeadModel.USERHED_PASSWORD;
txttPassword = txtPassword.Text.Trim();
if (txtPass == txtHoldStr)
{
MessageBox.Show("Login Successful");
this.Close();
}
else
{
MessageBox.Show("Invalid username or password please try again");
txtPassword.Focus();
}
}
catch (Exception ex) { }
}
}