1
votes

I am new to C#. I am trying to enter a password in textbox1 to login to my form. This login button enables certain controls. This works. Here is where the problem starts. I have made a change password section also where I enter the old password in textbox2, new password in textbox3 and confirm password in text box4. What I want is for the password to update from textbox3 or textbox4 and then be set as the password. So whatever was entered in textbox3 or textbox 4 is now the password. And when I enter this new changed password in textbox1 it logs in. I have tried everything I can think of with no solution. Here is the code I am using.

private void button14_Click(object sender, EventArgs e) // Main Screen Password Login Button
{
    String passWord;
    passWord = "login";

    if (textBox1.Text == passWord)
    {
        passWord = textBox3.Text;
        // textBox1.Clear();
        button1.Enabled = false;
        button2.Enabled = false;
        button3.Enabled = true;
        button4.Enabled = true;
        button5.Enabled = true;
        button6.Enabled = true;
        button7.Enabled = true;
        button8.Enabled = true;
        button9.Enabled = true;
        button10.Enabled = true;
        button11.Enabled = true;
        button12.Enabled = false;
        button16.Enabled = true;
        button16.Visible = true;
        button20.Enabled = true;
        numericUpDown1.Enabled = true;
        numericUpDown2.Enabled = true;

        button14.Click += ResetTimer;
    }

    else
    {
        MessageBox.Show("Password is Incorrect");
        textBox1.Clear();
    }
}
private void button19_Click(object sender, EventArgs e) // Admin Confirm Old Password Button
{
    //  String passWord;
    // passWord = textBox2.Text;

    if (textBox1.Text == textBox2.Text)
    {               
        //MessageBox.Show("Password is Correct");
        textBox3.Enabled = true;
        textBox4.Enabled = true;
    }
    else
    {
        MessageBox.Show("Password is Incorrect");
    }
private void button17_Click(object sender, EventArgs e) // Admin Update New password Button
{
    String passWord; 
    //passWord = textBox3.Text;

    // passWord = textBox3.Text;
    // passWord = textBox4.Text;

    if(textBox3.Text == textBox4.Text)
    {
        passWord = textBox3.Text;
        MessageBox.Show("Password Changed");
        textBox1.Clear();
        textBox2.Clear();
        textBox3.Clear();
        textBox4.Clear();
        textBox3.Enabled = false;
        textBox4.Enabled = false;

    }
    else
    {
        MessageBox.Show("Password Does Not Match");
        textBox2.Clear();
        textBox3.Clear();
        textBox4.Clear();
        textBox3.Enabled = false;
        textBox4.Enabled = false;
    }
    button17.Click += ResetTimer;
}

So when I click button17 I want the password to change to whatever is entered in textbox3 or textbox4. This new password will then be used to login at textbox1. I hope I described my problem correctly. Any help will be much appreciated. Thank you. Jennifer.

5
First I would suggest renaming all of your controls to something meaningful like oldPasswordTextBox, newPasswordTextBox, and confirmPasswordTextBox. It will make your code much more readable. - juharr
@juharr Yeah, I hate someControl1. - ikh

5 Answers

4
votes

In button17_Click() you have a local variable named passWord. When you assign your new value, you assign it to that local variable. In button19_Click() you have another - different - local variable with the same name. Please read MSDN on Variable and Method Scope immediately!

What you need is a global variable, which stores the valid password, and to remove all local variables with that name. Just use the global for the login and the change-process.

2
votes

Your problem is the variable scope. If you declare it inside a method, it will be only usable in a single execution of that method. If you have a password variable defined in button14_Click and a password variable defined again in button17_Click, these will be two different things and will exist only during the execution of given button's OnClick event (assuming these methods are properly assigned to event handlers).

// Move the password variable outside of the method scope,
// so it can be used by all buttonXX_Click methods.
private string password = "login";

private void button14_Click(object sender, EventArgs e) // Main Screen Password Login Button
{
    // No password variable defined here, instead we'll be using the one
    // we declared above.

    if (textBox1.Text == password)
    {
        // no changes here, omitted to make the answer shorter
    }
    else
    {
        MessageBox.Show("Password is Incorrect");
        textBox1.Clear();
    }
}

private void button19_Click(object sender, EventArgs e) // Admin Confirm Old Password Button
{
    // no changes here, omitted to make answer shorter
}
private void button17_Click(object sender, EventArgs e) // Admin Update New password Button
{
    if (textBox3.Text == textBox4.Text)
    {
        // By removing the local password variable and using the one
        // declared at the top, your change will be "remembered".

        password = textBox3.Text;
        MessageBox.Show("Password Changed");
        textBox1.Clear();
        textBox2.Clear();
        textBox3.Clear();
        textBox4.Clear();
        textBox3.Enabled = false;
        textBox4.Enabled = false;
    }
    else
    {
        MessageBox.Show("Password Does Not Match");
        textBox2.Clear();
        textBox3.Clear();
        textBox4.Clear();
        textBox3.Enabled = false;
        textBox4.Enabled = false;
    }
    button17.Click += ResetTimer;
}
1
votes

If I got it correctly, you have declared String passWord variable in button14_Click as well as in button17_Click methods.

Now the password value changed in button17_Click method will be in the scope of button17_Click only and not reflect in button14_Click declaration. Declare the passWord variable outside any method only. (at class level)

I guess this is learning project, because whenever you restart the application, the password will reset as per your declaration. For actual project you need to have something to store your user details like database. Hope this helps.

0
votes

I see you're starting with WinForms and C# programming in general.
As in the first answer, your passWord variable is local to methods button17_Click and button14_Click. First thing should be placing the password in some safe place. For starter, you could use "Properties -> Settings" in the project tree.
Create the "Password" entry of "String" type, assign an initial value for it, and then use this property in your code (instead of passWord variable). These properties are global to whole project. If you'd want to refer to "Password" property in these settings, you should refer to it by "Properties.Settings.Default.Password".
Hope this will help, but as I've wrote earlier - this is just for start.
And by the way - please name the controls (text boxes, buttons, etc.) meaningfully to keep your code clear and more readable.
Good luck!

0
votes

a few things - name your items such as textbox1 to something meaningful right now it's a jumble of a mess that is hard to follow.

It doesn't look like you're using any database to keep track of passwords, so i'm assuming you realize the password will be set back to "login" at each run of the application.

It looks like "button17_click" is likely your password change... you're using local variables "password" in two separate methods (button17_click and button14_click), they would have no knowledge of each other as they're locally scoped. If anything just make them variables to the class rather than the method, and that should solve your immediate problem.

private string Password = "login"

private void button14_Click(object sender, EventArgs e) // Main Screen Password Login Button
{

    if (textBox1.Text == PassWord)
    {
        Password = textBox3.Text;
        // textBox1.Clear();
        button1.Enabled = false;
        button2.Enabled = false;
        button3.Enabled = true;
        button4.Enabled = true;
        button5.Enabled = true;
        button6.Enabled = true;
        button7.Enabled = true;
        button8.Enabled = true;
        button9.Enabled = true;
        button10.Enabled = true;
        button11.Enabled = true;
        button12.Enabled = false;
        button16.Enabled = true;
        button16.Visible = true;
        button20.Enabled = true;
        numericUpDown1.Enabled = true;
        numericUpDown2.Enabled = true;

        button14.Click += ResetTimer;
    }

    else
    {
        MessageBox.Show("Password is Incorrect");
        textBox1.Clear();
    }
}

private void button17_Click(object sender, EventArgs e) // Admin Update New password Button
{

    if(textBox3.Text == textBox4.Text)
    {
        Password = textBox3.Text; // update the class variable Password to be the new password
        MessageBox.Show("Password Changed");
        textBox1.Clear();
        textBox2.Clear();
        textBox3.Clear();
        textBox4.Clear();
        textBox3.Enabled = false;
        textBox4.Enabled = false;

    }
    else
    {
        MessageBox.Show("Password Does Not Match");
        textBox2.Clear();
        textBox3.Clear();
        textBox4.Clear();
        textBox3.Enabled = false;
        textBox4.Enabled = false;
    }
    button17.Click += ResetTimer;
}