Hi all,

I have a login form and ChangePassword form. I want to retrieve the value of username entered in the Login form at the time of logging.

I have created a property named as RetUserName as follows:

public partial class frmLogin : Form
    {
        private string UseNam;
        public string RetUserName
        {
            get { return UseNam; }
            set { UseNam = value;}
        }
   }

In the button click event i have written the following code so that the value of the property is set:

this.RetUserName = (string)txtUserName.Text;

Now to retrieve the value of the property i have used the following coding in the form_load event of the changepassword form:

Form_Load Event:

frmLogin objLogin = new frmLogin();
        string UserName1 = objLogin.RetUserName;
        MessageBox.Show("NAME : " + UserName1);
        txtUserName.Text = UserName1;

Now when I execute the above program does not generate any errors, however the value entered by the user in the login form is not displayed in the changepassword form.

Can anyone let me know how should I change the coding so that I can retrieve the values entered by user in the login form to be displayed in the changepassword form?

Please help me! Thanks in advance!

Assuming you have this:

frmLogin objLogin = new frmLogin();
      string UserName1 = objLogin.RetUserName;
      MessageBox.Show("NAME : " + UserName1);
      txtUserName.Text = UserName1;

on different form than your first snippet, what your actually doing is creating a new copy of the first form, and getting your username from that, rather than the original form.

When you open the 2nd form, you will need to pass the reference to the first form to be able to access the information on it.

When opening form 2, you will need:

Form2 chngPwd = new Form2();
chngPwd.Show(this);

and to reference your property in form2

Form1 loginForm = (Form1)this.Parent;
MessageBox.Show("Username: " + main.RetUserName);

Assuming you have this:

frmLogin objLogin = new frmLogin();
      string UserName1 = objLogin.RetUserName;
      MessageBox.Show("NAME : " + UserName1);
      txtUserName.Text = UserName1;

on different form than your first snippet, what your actually doing is creating a new copy of the first form, and getting your username from that, rather than the original form.

When you open the 2nd form, you will need to pass the reference to the first form to be able to access the information on it.

When opening form 2, you will need:

Form2 chngPwd = new Form2();
chngPwd.Show(this);

and to reference your property in form2

Form1 loginForm = (Form1)this.Parent;
MessageBox.Show("Username: " + main.RetUserName);

Thanks for your prompt reply.

I have a MDI form that will be displayed after the user logs in successfully. and the menu control is used to open the changepassword form. When executing the program the first form is the login form, the second form is the mdi form and the mdi form consists of the menu control with the help of the menu controls the user can open the change password form

So in which form should i include the statement:
Form2 chngPwd = new Form2();
chngPwd.Show(this);

Please help me out I am totally confused. Please note that I have also tried using Public Static String variable but even that is not working out!

Thanks in advance!

Just put your string inside a class object and pass that to the form. The class object will be passed by reference and will still contain the changed value of your string after the form closes. For example:

public class Credentials
        {
            public string username;
            public Credentials(string username) {this.username = username;}
        }
        public Credentials myUser = new Credentials("DonaldDuck");
        Form2 form2 = new Form2(myUser);
        // when form2 returns below, myUser will have any changes that were made in form2
        Form2.ShowDialog();
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.