如何正确打开登录表单

How to properly open a login form

我的 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) { }

            }
        }

在您的原始代码中,检查对话结果。

private void MDiMain_Load(object sender, EventArgs e)
{
    setDisplaysize();

    Form newLogin = new FormControllers.FrmLogin();
    newLogin.StartPosition = FormStartPosition.CenterScreen;

    if (newLogin.ShowDialog(this) != DialogResult.OK)
    {
        Close();
        // or better:
        // BeginInvoke((Action)Close);
        return;
    }

    // possibly further main form initialization logic here
  } 

您需要设置对话结果:

if (txtPass == txttPassword)
{
    MessageBox.Show("Login Successful");
    DialogResult = DialogResult.OK;
    Close();
}

只有默认按钮会自动为您执行此操作。当涉及逻辑时,您需要根据身份验证的结果对其进行设置 - 在这种情况下 - 身份验证。

除此之外,我猜测与原始代码中的 txtHoldStr 的比较是错误的。此变量始终为空。要检查文本框中的密码是否与数据模型中的密码匹配,请将 txtPass 与 txttPassword 进行比较。