【问题标题】:C# Password Help Needed需要 C# 密码帮助
【发布时间】:2015-08-27 01:37:50
【问题描述】:

我是 C# 新手。我正在尝试在 textbox1 中输入密码以登录我的表单。此登录按钮启用某些控件。这行得通。这是问题开始的地方。我也做了一个更改密码部分,我在 textbox2 中输入旧密码,在 textbox3 中输入新密码并在 textbox4 中确认密码。我想要的是从 textbox3 或 textbox4 更新密码,然后设置为密码。因此,在 textbox3 或 textbox 4 中输入的任何内容现在都是密码。当我在 textbox1 中输入这个新更改的密码时,它会登录。我已经尝试了所有我能想到的方法,但没有解决方案。这是我正在使用的代码。

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

因此,当我单击 button17 时,我希望密码更改为 textbox3 或 textbox4 中输入的任何内容。然后,这个新密码将用于在 textbox1 登录。我希望我正确地描述了我的问题。任何帮助都感激不尽。谢谢你。 詹妮弗。

【问题讨论】:

  • 首先,我建议将所有控件重命名为有意义的名称,例如 oldPasswordTextBox、newPasswordTextBox 和 confirmPasswordTextBox。它将使您的代码更具可读性。
  • @juharr 是的,我讨厌 someControl1。

标签: c#


【解决方案1】:

一些事情 - 将您的项目(例如 textbox1)命名为现在有意义的名称,这是一团难以理解的混乱。

您似乎没有使用任何数据库来跟踪密码,所以我假设您意识到密码将在每次运行应用程序时设置回“登录”。

看起来“button17_click”可能是您的密码更改...您在两个单独的方法(button17_click 和 button14_click)中使用局部变量“password”,因为它们是本地范围的,所以它们彼此不知道.如果有什么只是使它们成为类而不是方法的变量,那应该可以解决您的直接问题。

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

【讨论】:

  • 没问题,如果您还没有查看 Patrik 答案中发布的链接,它可能有助于进一步了解为什么答案对您的问题有效。将控件重命名为有意义的内容将帮助您处理智能感知等问题,并帮助将来查看您的代码的其他人更轻松地遵循它。
  • @Jennifer21 没问题,您需要在网站上提高一点声望才能进行上下投票。见:stackoverflow.com/help/privileges
【解决方案2】:

button17_Click() 中有一个名为passWord 的局部变量。当您分配新值时,您将其分配给该局部变量。在button19_Click() 中,您有另一个 - 不同 - 同名的局部变量。请立即阅读MSDN on Variable and Method Scope

您需要的是一个全局变量,它存储有效密码,并删除所有具有该名称的局部变量。只需将全局用于登录和更改过程。

【讨论】:

    【解决方案3】:

    您的问题是变量范围。如果您在方法中声明它,它将仅在该方法的单次执行中可用。如果您在 button14_Click 中定义了密码变量,在 button17_Click 中再次定义了密码变量,这将是两个不同的东西,并且仅在给定按钮的 OnClick 事件执行期间存在(假设这些方法已正确分配给事件处理程序)。

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

    【讨论】:

      【解决方案4】:

      我看到您一般是从 WinForms 和 C# 编程开始的。
      与第一个答案一样,您的 passWord 变量是方法 button17_Click 和 button14_Click 的本地变量。首先应该将密码放在某个安全的地方。首先,您可以在项目树中使用“属性 -> 设置”。
      创建“String”类型的“Password”条目,为其分配一个初始值,然后在您的代码中使用此属性(而不是 passWord 变量)。这些属性对整个项目都是全局的。如果您想在这些设置中引用“密码”属性,则应通过“Properties.Settings.Default.Password”引用它。
      希望这会有所帮助,但正如我之前所写的 - 这只是开始。
      顺便说一下 - 请有意义地命名控件(文本框、按钮等),以使您的代码清晰易读。
      祝你好运!

      【讨论】:

        【解决方案5】:

        如果我没记错的话,你已经在 button14_Clickbutton17_Click 方法中声明了 String passWord 变量。

        现在button17_Click 方法中更改的密码值将仅在button17_Click 的范围内,而不会反映在button14_Click 声明中。 only 在任何方法之外声明 passWord 变量。 (在课堂上)

        我猜这是学习项目,因为每当您重新启动应用程序时,密码都会根据您的声明重置。对于实际项目,您需要有一些东西来存储您的用户详细信息,例如数据库。 希望这会有所帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-08-12
          • 1970-01-01
          • 2018-09-30
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多