c# - 如何在c#中实现没有更多实例的登录表单和主表单

标签 c# winforms authentication

我在单击登录按钮时为主窗体创建一个实例,并在单击注销按钮时再次为登录窗体创建实例。我的代码是

      if ((txtUsrNm.Text == "Admin") && (txtPswd.Text == "Admin"))
        {
            mainForm mainFm = new mainForm();
            mainFm.userNam = txtUsrNm.Text;
            mainFm.pasWrd = txtPswd.Text;
            mainFm.Show();
            this.Hide();
        }
         else
        {
            MessageBox.Show("Invalid Username or Password.");
        }

再次在注销按钮中

        this.Hide();
        loginFrm lgn = new loginFrm();
        lgn.Show();

如果我尝试关闭而不是隐藏,整个应用程序将关闭。 通过这样做,Application.OpenForms 计数在每次登录和注销操作时都会增加。如何在windows应用程序中实现登录和注销操作。是否有任何其他实现此操作的想法。谢谢你

最佳答案

在调用 Application.Run() 之前,您需要将登录部分放在 Program.cs 中 例如:

 if (new frmLogin().ShowDialog() == DialogResult.OK)
 {
     Application.Run(new frmMain());
 }

然后在登录表单中输入登录代码。用户登录后,表单登录将关闭并设置 this.dialogresualt = dialogresult.ok; 例如:

if (txtUserName.Text == "blah"
      && txtPassword.Text == "blah")
   {
     txtPassword.BackColor = Color.YellowGreen;
     txtUserName.BackColor = Color.YellowGreen;
     this.DialogResult = DialogResult.OK;
   }
   else
   {
     txtPassword.BackColor = Color.Salmon;
     txtUserName.BackColor = Color.Salmon;
    }

关于c# - 如何在c#中实现没有更多实例的登录表单和主表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14375892/

相关文章:

c# - 禁用保存文件对话框的文件名框

php - 将旧用户迁移到 symfony2

flash - 使用 Flash 进行基本身份验证

c# - 如何在 Web Api OAuth 中获取访问 token ?

c# - 如何为我的场景替换 if 循环逻辑?

c# - 用户定义的类名与内置类名冲突 - C#

c# - 窗体 : How to implement button with correct background colors

c# - Entity Framework DbContext : Value cannot be null, 参数名称来源

c# - .NET 类重构困境

c# - Active TabPage 与我的控件颜色相同