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.

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.

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?

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!
        }

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?

yes.
The is how the data (parameters) are passed in OOP.
You can check more about passing parameters here.

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.

//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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.