简体   繁体   English

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

[英]Pass a value from login form to main form

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

What would be the best way to pass value from loginForm to Program.cs and myForm? 将值从loginForm传递到Program.cs和myForm的最佳方法是什么?

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

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). 这样,您将能够在关闭表单后将其获取(它仍将保留在内存中)。

it is simply use EF on your codes just like below 只需在代码上使用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();

    }
  1. This is the best way to transfer data from one form to another, On the LoginForm.cs write like this: 这是将数据从一种形式传输到另一种形式的最佳方法,在LoginForm.cs上这样写:

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

    public MainForm () { } 公共MainForm(){}

like this: 像这样:

public MainForm(string userName,string password){
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM