2

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!

1
  • implement a shared or static property... that is the trick.. your code will instantiate a new object frmLogin objLogin = new frmLogin(); Commented Sep 15, 2009 at 8:06

3 Answers 3

1

You are actualy creating a new instance of your login form and that instance doesn't know your entered user name.

I would suggest you add a property UserName to your changepassword form. and in the calling code for your forms you assign the values. Someting like this:

//create and show login dialog
frmLogin objLogin = new frmLogin();
objLogin.ShowDialog();

//create changepassword form       
frmChangePwd objChangePwd = new frmChangePwd();
objChangePwd.UserName = objLogin.RetUserName;
objChangePwd.ShowDialog();
Sign up to request clarification or add additional context in comments.

Comments

0

You are creating a local variable of login form, so it will get destroyed after its scope and along with it the value of UseNam will also get lost. You have to store it to something that is accessible at a point where you want to use it.

You can do it by following way also ( along with other ways :)

// This class is mainly used to transfer values in between different components of the system
    public class CCurrent
    {
        // Currently logged in user
        public static string UserName = "";

        // Indicates to whole system whether database is Valid and connectable or not ?
        public static Boolean DatabaseValid = false;
        // Indicates whether we are able to connect databse or not.
        public static Boolean DatabaseConnectable = false;

    }

Now in your code you can do following :

    frmLogin objLogin = new frmLogin();
    objLogin.ShowDialog();
    CCurrent.UserName = objLogin.RetUserName;
    MessageBox.Show("NAME : " + CCurrent.UserName);
    txtUserName.Text = CCurrent.UserName;

Comments

0

How are you displaying the objLogin form?
There should somehitng like

objLogin.ShowDialog();

in your code...

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.