简体   繁体   English

如何在列表框C#中显示登录的用户

[英]How to display the logged user in listbox c#

So i have a login system and when the user logins a new form opens and i want the form to display the user Name. 所以我有一个登录系统,当用户登录时,会打开一个新表格,我希望该表格显示用户名。 When user register he have to put his name and surname and that 2 things i want to be displayed in the new form that opens. 当用户注册时,他必须输入他的名字和姓氏,而这2件事我想在打开的新表格中显示。 So is there any easy way to put the logined user name into listbox? 那么,有什么简单的方法可以将登录的用户名放入列表框?

so here is the registration code: 所以这是注册码:

MySqlConnection dataConnection = new MySqlConnection();
        dataConnection.ConnectionString = "datasource=localhost;port=3306;username=root;password=";
        dataConnection.Open();
        MySqlTransaction transakcija = dataConnection.BeginTransaction();
        MySqlCommand dataCommand = new MySqlCommand();
        dataCommand.Connection = dataConnection;
        dataCommand.Transaction = transakcija;
        try
        {
            dataCommand.CommandText = "Insert INTO login.users (ime,upIme,geslo,dovoljenja) VALUES ('" + this.tB_Ime.Text + "','" + this.tB_upIme.Text + "','" + this.tB_geslo.Text + "', 'Navaden uporabnik')";
            dataCommand.CommandType = CommandType.Text;
            dataCommand.ExecuteNonQuery();
            transakcija.Commit();
            MessageBox.Show("Registracija uspešna!");
            this.Hide();
        }
        catch (Exception eks)
        {
            transakcija.Rollback();
            MessageBox.Show("Napaka pri registraciji\n" + eks.Message);
        }
        finally
        {
            dataCommand.Connection.Close();
        }
        Form1 f1 = new Form1();
        f1.ShowDialog();
        this.Close();

and login: 并登录:

try
        {
            string myConnection = "datasource=localhost;port=3306;username=root;password=";
            MySqlConnection myConn = new MySqlConnection(myConnection);

            MySqlCommand SelectCommand = new MySqlCommand(" select * from login.users where upIme='" + this.tB_upIme.Text + "' AND geslo='" + this.tB_geslo.Text + "' ;", myConn);

            MySqlDataReader myReader;
            myConn.Open();
            myReader = SelectCommand.ExecuteReader();
            int count = 0;
            bool IsAdminUser = false;
            while (myReader.Read())
            {
                count = count + 1;
                IsAdminUser = myReader["dovoljenja"].Equals("Admin");
            }
            if (count == 1 && IsAdminUser == true)
            {
                MessageBox.Show("Prijavljeni ste kot administrator!");
                this.Hide();
                Form4 f4 = new Form4();
                f4.ShowDialog();
            }
            else if (count == 1)
            {
                MessageBox.Show("Uspešno ste se prijavili!");
                this.Hide();
                Form3 f3 = new Form3();
                f3.ShowDialog();
            }
            else if (count > 1)
            {
                MessageBox.Show("Dvojno uporabniško ime in geslo!");
                this.Hide();
            }
            else
                MessageBox.Show("Uporabniško ime ali geslo ni pravilno!");
            myConn.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

you can send the LoggedIn UserName to the UserForm constructor and from there you can add it to the required control. 您可以将LoggedIn UserName发送到UserForm构造函数,然后从那里将其添加到所需的控件中。

Try This: 尝试这个:

Login Form: 登录表单:

        string LoggedInUserName = String.Empty;
        while (myReader.Read())
        {
            count = count + 1;
            IsAdminUser = myReader["dovoljenja"].Equals("Admin");
            LoggedInUserName = myReader["FirstName"].ToString()+
                               myReader["LastName"].ToString();
        }
        if (count == 1 && IsAdminUser == true)
        {
            MessageBox.Show("Prijavljeni ste kot administrator!");
            this.Hide();
            Form4 f4 = new Form4(LoggedInUserName);
            f4.ShowDialog();
        }
        else if (count == 1)
        {
            MessageBox.Show("Uspešno ste se prijavili!");
            this.Hide();
            Form3 f3 = new Form3(LoggedInUserName);
            f3.ShowDialog();
        }

Now change the both admin form(Form4) and UserForm (Form3) 现在同时更改管理表单(Form4)和用户表单(Form3)

    //now change the Admin Form Form4 constructor to take 1 argument
    public Form4(string username)
    {
        InitializeComponent();
        myListBox.Items.Add(username);//Label1.Text= username;
    }

   //now change the UserForm Form3 constructor to take 1 argument
    public Form3(string username)
    {
        InitializeComponent();
        myListBox.Items.Add(username);//Label1.Text= username;
    }

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

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