Pass a value from login form to main form

后端 未结 4 2021
礼貌的吻别
礼貌的吻别 2021-01-26 09:26

I have a login screen and I need to pass username to my main form (for getting permissions etc.). Here is my code:

//Login
private void button1_Click(object send         


        
相关标签:
4条回答
  • 2021-01-26 10:06
    1. This is the best way to transfer data from one form to another, On the LoginForm.cs write like this:

      ex.UserName = txtUserName.text;    
      Password=txtPassword.text;    
      MainForm mainForm = new MainForm(UserName,Password);    
      this.Hide(); 
      
      mainForm.Show();
      
    2. In the MainForm.cs edit the

      public MainForm () { }

    like this:

    public MainForm(string userName,string password){
    }
    
    0 讨论(0)
  • 2021-01-26 10:12

    In login form

    public string UserName {get; private set;}
    
    if (string.Compare(dbPassword, appPassword) == 0)
                    {
                        UserName = txtUser.Text;
                        //I need to pass username value to myForm...
                        DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        //Show warning
                    }
    

    in main

        DialogResult result;
        using (var loginForm = new Login())
            result = loginForm.ShowDialog();
        if (result == DialogResult.OK)
        {
            var username = loginForm.UserName;
            Application.Run(new myForm(username));
        }
    
    0 讨论(0)
  • 2021-01-26 10:12

    Expose username as a string property of your login form class. This way you'll be able to fetch it after the form will be closed (it will still remain in memory).

    0 讨论(0)
  • 2021-01-26 10:16

    it is simply use EF on your codes just like below

      }
        Siman_dbEntities db = new Siman_dbEntities();
        public string UserNameLogedIn;
        private void btnLogin_Click(object sender, EventArgs e)
        {
        var login = from b in db.Tbl_Users.Where(b => b.Username == txtUsername.Text && b.Password == txt_Password.Text)
                        select b;
            if (login.Count()==1)
            {
                this.Hide();
                main frmmain = new main();
                frmmain.Show();
            }
            var query = db.Tbl_Users.Where(c => c.Username == txtUsername.Text).Single();
            UserNameLogedIn = query.Name.ToString();
    
        }
    
    0 讨论(0)
提交回复
热议问题