一旦插入用户名和密码,相关表格就不会加载。只有登录屏幕被隐藏/关闭。用户角色/密码/ everythin是100%正确的。 “角色”的输出也是正确的。 请指教。
private void btnLogin_Click(object sender, EventArgs e)
{
try
{
DataTable result = uc.Login(tbxUserName.Text, tbxPassword.Text);
if (result.Rows.Count == 1)
{
this.Hide();
string role = result.Rows[0]["Role"].ToString();
switch (role)
{
case "User":
FrmUser fuser = new FrmUser();
fuser.ShowDialog();
this.Close();
break;
case "Admin":
FrmMain fmain = new FrmMain();
fmain.ShowDialog();
this.Close();
break;
}
}
else
{
MessageBox.Show ("Invalid User Name or Password", "Incorrect Login Details",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
答案 0 :(得分:0)
我注意到,不是在每个角色上执行this.Close()
关闭登录表单,而是在成功登录后隐藏该登录表单:
if (result.Rows.Count == 1)
{
this.Hide();
string role = result.Rows[0]["Role"].ToString();
switch (role)
{
case "User":
FrmUser fuser = new FrmUser();
fuser.ShowDialog();
// note that this.Close() removed
break;
case "Admin":
FrmMain fmain = new FrmMain();
fmain.ShowDialog();
// note that this.Close() removed
break;
}
}
else
{
MessageBox.Show ("Invalid User Name or Password", "Incorrect Login Details",MessageBoxButtons.OK,MessageBoxIcon.Error);
Application.Exit(); // optional, use if you want to exit all app window when wrong credentials supplied
}
并将关闭处理程序放在用户和&而不是主要形式,因此关闭其中一个也将关闭隐藏的登录表单:
private void FrmUser_FormClosed(object sender, FormClosedEventArgs e)
{
this.Close();
}
private void FrmMain_FormClosed(object sender, FormClosedEventArgs e)
{
this.Close();
}
参考:When this.Close() executed all forms close?
希望你的问题得到解决,CMIIW。
答案 1 :(得分:0)
实际上这很奇怪。你能检查一下Form是否已经打开并且某种方式失去了Focus?您可以在此声明后面添加以下代码段:
fuser.ShowDialog();
检查表单是否仍处于打开状态的代码:
if (Application.OpenForms[fuser.Name] == null)
{
MessageBox.Show("Form is not open");
}
希望这可能会有所帮助。