我現在有一個奇怪的問題,現在在我的程序中登錄窗體。基本上,如果用戶輸入不正確的登錄名,然後點擊進入彈出窗口,則關閉它並再次提交日誌。有沒有辦法阻止這種情況?所以當用戶按下回車鍵關閉彈出窗口時,輸入按鍵不會以我的登錄形式註冊?點擊輸入對話框無法正常工作
繼承我的登錄表單中的代碼。 Button1是登錄按鈕謝謝!
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox2.Text == "")
{
MessageBox.Show("Please enter a username and password!");
}
else
{
bool loggedin = false;
for (int i = 0; i <= Program.ProfileList.Count - 1; i++)
{
if (Program.ProfileList[i].Username == textBox1.Text)
{
AESEncrypt encrypt = new AESEncrypt();
if (Program.ProfileList[i].Password == encrypt.EncryptToString(textBox2.Text))
{
MessageBox.Show("Succesfully logged in!");
Program.ActiveUser = textBox1.Text;
loggedin = true;
this.Close();
}
}
}
if (!loggedin)
{
MessageBox.Show("Incorrect username or password!");
}
}
}
private void textBox2_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
button1.PerformClick();
}
}
我不是很確定你說的是發生了什麼,但我覺得你的問題在於,在'Form.AcceptButton'指定按鈕的事實您的表單是您按Enter鍵時按下的按鈕。所以如果你不想要這種行爲,你需要清除「Form.AcceptButton」的值。 – Pete
此外,你不會得到一個'KeyUp'爲回車鍵,如果你擁有的AcceptButton集,但如果我沒有記錯,你會得到一個'KeyDown'它。 – Pete