繁体   English   中英

需要C#密码帮助

[英]C# Password Help Needed

我是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。 我希望我正确地描述了我的问题。 任何帮助都感激不尽。 谢谢。 珍妮佛

button17_Click()您有一个名为passWord的局部变量。 分配新值时,将其分配给该局部变量。 button19_Click()您还有另一个具有相同名称的不同局部变量。 请立即阅读有关变量和方法范围的MSDN

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

您的问题是可变范围。 如果在方法内部声明它,则只能在该方法的一次执行中使用。 如果您在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;
}

如果我正确button14_Click它,则可以在button14_Clickbutton17_Click方法中声明String passWord变量。

现在改了密码值button17_Click方法将在范围button17_Click只有在不能反映button14_Click声明。 在任何方法外部声明passWord变量。 (在课堂上)

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

一些事情-现在将您的项目(例如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;
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM