0

I have three forms, the login form, main menu, and user profile. When a user logged in and clicks the user profile button from the main menu, the information of the logged in account must display.

I do it by passing the username from the log in form to the main menu form into a label;thus, if the user clicks the user profile button, the username label from the main menu will be pass again and execute a query from the database "select lastname, firstname from useraccount where username="usernamelabel.Text". Though I doubt that this is the reliable and easiest way of doing this.

Edited by AngelicOne: n/a

2
Contributors
7
Replies
8
Views
6 Years
Discussion Span
Last Post by Mitja Bonca
1

Login form has to appear before MainForm.Take a look into this code, how to pass data:

//program.cs (where is the Main() method):
    using (Login login = new Login())
    {
        login.StartPosition = FormStartPosition.CenterScreen;
        if (login.ShowDialog() == DialogResult.OK)
        {                        
             Application.Run(new Form1(login.strUserName));
        }
    }

    //LOGIN FORM: 
    public partial class Login : Form
    {
        public string strUserName { get; private set; }

        public Login()
        {           
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string strUser = textBox1.Text;
            string strPswd = textBox2.Text;
            if (strUser != String.Empty && strPswd != String.Empty)
            {
                //YOUR METHOD TO CHECK LOGIN DATA:
                if (LoginCheck.CheckingLogin(strUser, strPswd) == true)
                {
                    strUserName = strUser;
                    this.DialogResult = DialogResult.OK;
                    //IF THEY ARE OK, CODE RETURNS TO MAIN METHOD (on program.cs)
                }
            }
        }

     //ON FORM1:
     public partial class Form1 : Form
    {        
        public Form1(string _strUser) //passing data from main to the constructor!
        {            
            InitializeComponent();
            OnBeginning(_strUser);
        }

        private void OnBeginning(string strUserName)
        {
            label1.Text = "Loged user: " + strUserName;
        }

Hope this helps explaining how you should do the login and passing data (username) between classes and methods inside classes.

0

Yes, that resembles my code for passing data, but what if I don't need to show the logged in user on the main menu? Do I just set the visibility of the label to false?

0

Do like this:

public Form1(string _strUser) //passing data from main to the constructor!
        {            
            InitializeComponent();
            //OnBeginning(_strUser); comment this line, so it won`t be called!
        }
0

Does that mean that this line of code

public Form1(string _strUser)

is the one necessary for passing data; while this code

OnBeginning(_strUser);

uses the passed data for an object like label?

0

Last question, how to pass the data again from the passed data in form1? Because there's a button in form1 to go to form2 that will show the profile of who is logged in.

0
//form1:
private string userName; //class variable
//from1 constructor:
public Form1(string passedValue)
{
     InitializeComponent();
     userName = passedValue; //here you bind a parameter passed into construncot to class variable
}

private void buttonOpenForm2(object sender, EventArgs e)
{
     //USE ONE OR THE OTHER WAY (NOT BOTH, BECUASE YOU WILL GET AN ERROR)
     //1. WAY:
     Form2 form2 = new Form2(userName);
     form2.Show(this);

     //2. WAY:
     Form2 form2 = new Form2();
     form2.ShowingUserName = userName;
     form2.Show(this);
}


//form2:
public Form2(string passedValue)  //1ST WAY (IF 2ND. DELETE THE PARAMETER IN BRACKETS)
{
     InlitializeConponent();
     label1.Text = passedValue; //pass the parameter value to label
}

public void ShowingUserName(string value) //2ND. WAY!
{
     label1.Text = value;
}

Hope this helps explaining how to pass parameters between forms (and methods).
Mitja

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.