将值从登录表单传递到主表单

时间:2014-02-17 20:06:23

标签: c# winforms

我有一个登录屏幕,我需要将用户名传递给我的主表单(获取权限等)。这是我的代码:

//Login
private void button1_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(txtUser.Text))
        {
            //Show warning
        }
        else if (string.IsNullOrEmpty(txtPass.Text))
        {
            //Show warning
        }

        using (DataTable dt = LookupUser(txtUser.Text)) //Look into SQL data table for username and password
        {
            if (dt.Rows.Count == 0)
            {
                //Show warning
            }
            else
            {
                string dbPassword = Convert.ToString(dt.Rows[0]["pass"]);
                string appPassword = Encrypt(txtPass.Text);
                if (string.Compare(dbPassword, appPassword) == 0)
                {
                    //I need to pass username value to myForm...
                    DialogResult = DialogResult.OK;
                }
                else
                {
                    //Show warning
                }
            }
        }

//Program.cs
static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        DialogResult result;
        using (var loginForm = new Login())
            result = loginForm.ShowDialog();
        if (result == DialogResult.OK)
        {
            Application.Run(new myForm());
        }
    }

将值从loginForm传递到Program.cs和myForm的最佳方法是什么?

4 个答案:

答案 0 :(得分:3)

以登录表格

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
                }

中的

    DialogResult result;
    using (var loginForm = new Login())
        result = loginForm.ShowDialog();
    if (result == DialogResult.OK)
    {
        var username = loginForm.UserName;
        Application.Run(new myForm(username));
    }

答案 1 :(得分:1)

将username作为登录表单类的字符串属性。通过这种方式,您可以在表单关闭后获取它(它仍将保留在内存中)。

答案 2 :(得分:0)

只需在代码上使用EF即可 就像下面一样

  }
    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();

    }

答案 3 :(得分:0)

  1. 这是将数据从一种形式传输到另一种形式的最佳方法,在LoginForm.cs上这样写:

    ex.UserName = txtUserName.text;    
    Password=txtPassword.text;    
    MainForm mainForm = new MainForm(UserName,Password);    
    this.Hide(); 
    
    mainForm.Show();
    
  2. 在MainForm.cs中编辑

    公共MainForm() { }

像这样:

public MainForm(string userName,string password){
}