1

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

            }
        }
4

2 回答 2

2

您需要设置对话结果:

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

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

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

于 2018-12-12T14:36:53.127 回答
1

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

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
  } 
于 2018-12-12T14:25:38.370 回答