C# 需要密码帮助吗
我是C的新手。我正在尝试在textbox1中输入密码以登录到我的表单。此登录按钮启用某些控件。这很有效。问题就从这里开始。我在文本框2中输入了旧密码,在文本框3中输入了新密码,在文本框4中输入了确认密码。我想要的是从textbox3或textbox4更新密码,然后将其设置为密码。因此,无论在文本框3或文本框4中输入什么,现在都是密码。当我在textbox1中输入这个新修改的密码时,它会登录。我已经尝试了我能想到的一切,但没有解决办法。这是我正在使用的代码C# 需要密码帮助吗,c#,C#,我是C的新手。我正在尝试在textbox1中输入密码以登录到我的表单。此登录按钮启用某些控件。这很有效。问题就从这里开始。我在文本框2中输入了旧密码,在文本框3中输入了新密码,在文本框4中输入了确认密码。我想要的是从textbox3或textbox4更新密码,然后将其设置为密码。因此,无论在文本框3或文本框4中输入什么,现在都是密码。当我在textbox1中输入这个新修改的密码时,它会登录。我已经尝试了我能想到的一切,但没有解决办法。这是我正在使用的代码 private void button1
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时,我希望密码更改为textbox3或textbox4中输入的任何内容。然后将使用此新密码登录textbox1。我希望我正确地描述了我的问题。任何帮助都将不胜感激。非常感谢。
珍妮弗。一些事情-将你的项目命名为有意义的东西,比如textbox1,现在它是一个混乱的混乱,很难理解 看起来您没有使用任何数据库来跟踪密码,因此我假设您意识到在应用程序每次运行时密码都会被设置回登录 看起来按钮17_单击可能是您的密码更改。。。如果您在两个单独的方法button17\u click和button14\u click中使用局部变量密码,则它们将互不了解,因为它们是局部作用域。如果有什么问题的话,只需将它们作为类的变量,而不是方法的变量,这将解决您眼前的问题
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;
}
有几件事-将你的项目命名为textbox1等有意义的东西现在它是一团混乱,很难理解 看起来您没有使用任何数据库来跟踪密码,因此我假设您意识到在应用程序每次运行时密码都会被设置回登录 看起来按钮17_单击可能是您的密码更改。。。如果您在两个单独的方法button17\u click和button14\u click中使用局部变量密码,则它们将互不了解,因为它们是局部作用域。如果有什么问题的话,只需将它们作为类的变量,而不是方法的变量,这将解决您眼前的问题
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;
}
在按钮17中,单击您有一个名为passWord的局部变量。分配新值时,将其分配给该局部变量。在按钮19中,单击您有另一个同名的-不同-局部变量。请立即阅读
您需要的是一个全局变量,它存储有效密码,并删除所有具有该名称的局部变量。只需将全局变量用于登录和更改过程。在按钮17中,单击您有一个名为passWord的局部变量。分配新值时,将其分配给该局部变量。在按钮19中,单击您有另一个同名的-不同-局部变量。请立即阅读
您需要的是一个全局变量,它存储有效密码,并删除所有具有该名称的局部变量。只需将全局变量用于登录和更改过程。您的问题在于变量范围。如果在一个方法中声明它,它将只在该方法的一次执行中可用。如果在button14\u Click中定义了密码变量,在button17\u 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\u Click中定义了密码变量,在button17\u 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;
}
我知道您一般都是从WinForms和C编程开始的。 与第一个答案一样,您的密码变量是button17\u Click和button14\u Click方法的本地变量。首先应该把密码放在安全的地方。对于初学者,可以在项目树中使用属性->设置。 创建字符串类型的密码条目,为其指定初始值,然后在代码中使用此属性而不是密码变量。这些属性是整个项目的全局属性。如果要在这些设置中引用Password属性,应通过Properties.settings.Default.Password引用它。
希望这会有所帮助,但正如我之前所写的,这只是开始。顺便说一句,请为控件文本框、按钮等命名,以保持代码清晰易读。祝你好运 我知道您一般都是从WinForms和C编程开始的。 与第一个答案一样,您的密码变量是方法button17\u单击并单击按钮14的本地变量 _点击。首先应该把密码放在安全的地方。对于初学者,可以在项目树中使用属性->设置。 创建字符串类型的密码条目,为其指定初始值,然后在代码中使用此属性而不是密码变量。这些属性是整个项目的全局属性。如果要在这些设置中引用Password属性,应通过Properties.settings.Default.Password引用它。
希望这会有所帮助,但正如我之前所写的,这只是开始。顺便说一句,请为控件文本框、按钮等命名,以保持代码清晰易读。祝你好运 如果我没有弄错,您已经在button14\u-Click和button17\u-Click方法中声明了字符串密码变量 现在,在button17_Click方法中更改的密码值将仅在button17_Click的范围内,而不会反映在button14_Click声明中。 仅在任何方法之外声明密码变量。在班级层面 我猜这是一个学习项目,因为每当你重新启动应用程序时,密码都会按照你的声明重置。对于实际的项目,您需要一些东西来存储您的用户详细信息,比如数据库。
希望这有帮助。如果我理解正确,您已经在button14\u Click和button17\u Click方法中声明了字符串密码变量 现在,在button17_Click方法中更改的密码值将仅在button17_Click的范围内,而不会反映在button14_Click声明中。 仅在任何方法之外声明密码变量。在班级层面 我猜这是一个学习项目,因为每当你重新启动应用程序时,密码都会按照你的声明重置。对于实际的项目,您需要一些东西来存储您的用户详细信息,比如数据库。
希望这有帮助。首先,我建议将所有控件重命名为有意义的控件,如oldPasswordTextBox、newPasswordTextBox和confirmPasswordTextBox。这将使您的代码更具可读性。@juharr是的,我讨厌某些控件1。首先,我建议您将所有控件重命名为有意义的控件,如oldPasswordTextBox、newPasswordTextBox和confirmPasswordTextBox。这将使您的代码更具可读性。@juharr是的,我讨厌someControl1。没问题,如果您没有查看Patrik答案中的链接,它可能有助于进一步了解为什么答案比您问题中的答案更有效。将控件重命名为有意义的控件将帮助您实现智能感知等功能,并帮助将来查看您的代码的其他人更轻松地使用它。@Jennifer 21没问题,您需要在网站上获得更高的声誉,然后才能上下投票。请看:没问题,如果您没有查看Patrik答案中的链接,这可能有助于进一步了解为什么答案比您问题中的答案更有效。将控件重命名为有意义的控件将帮助您实现智能感知等功能,并帮助将来查看您的代码的其他人更轻松地使用它。@Jennifer 21没问题,您需要在网站上获得更高的声誉,然后才能上下投票。见: