如何实现登录表单,并主要形式,而不在C#中多个实例 [英] How to Implement login form and main form without more instances in c#

查看:88
本文介绍了如何实现登录表单,并主要形式,而不在C#中多个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

荫创建一个实例上登录按钮点击的主要形式,并再次创建实例来登入注销按钮单击窗体。我的代码是

Iam creating an instance to the main form on login button click and again creating instance to login form on logout button click. my code is

      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应用程序注销操作。是否有任何其他的想法执行这项操作。感谢你

If I try to close instead of Hide, the whole application is closing. By doing this Application.OpenForms count is increasing on every login and logout operations. How to implement the login and logout operations in windows application. Is there any other Idea for Implementing this operation. Thanking you

推荐答案

您需要把您的登录部分Program.cs中,前Application.Run()呼吁
例如:

You need to put your login part in Program.cs , before Application.Run() called e.g :

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



然后把登录密码在登录表单。用户登录后,表单登录将被关闭,将设置this.dialogresualt = dialogresult.ok;
例如:

then put login codes in login form. after user logged in, form login will be closed and will set this.dialogresualt = dialogresult.ok; e.g :

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#中多个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆