[英]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_Click
和button17_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.