需要 C# 密码帮助
本文关键字:帮助 密码 需要 | 更新日期: 2023-09-27 17:55:27
我是C#的新手。我正在尝试在文本框 1 中输入密码以登录到我的表单。此登录按钮启用某些控件。这行得通。这就是问题开始的地方。我也做了一个更改密码部分,我在文本框2中输入旧密码,在文本框3中输入新密码,并在文本框4中确认密码。我想要的是密码从文本框3或文本框4更新,然后设置为密码。因此,在文本框 3 或文本框 4 中输入的任何内容现在都是密码。当我在文本框1中输入这个新更改的密码时,它会登录。我已经尝试了我能想到的一切,但没有解决方案。这是我正在使用的代码。
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;
}
因此,当我单击按钮17时,我希望密码更改为在文本框3或文本框4中输入的任何密码。然后,此新密码将用于在文本框1登录。我希望我正确地描述了我的问题。任何帮助将不胜感激。谢谢。詹妮弗。
在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
和button17_Click
方法中声明了String passWord
变量。
现在,button17_Click
方法中更改的密码值将仅在button17_Click
范围内,而不会反映button14_Click
声明中。仅在任何方法外部声明 passWord 变量。(班级级别)
我想这是学习项目,因为每当您重新启动应用程序时,密码都会根据您的声明重置。对于实际项目,您需要有一些东西来存储您的用户详细信息,例如数据库。希望这有帮助。
我看到你从WinForms和C#编程开始。
与第一个答案一样,您的 passWord 变量是方法 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;
}