2013-11-26 43 views
1

我現在有一個奇怪的問題,現在在我的程序中登錄窗體。基本上,如果用戶輸入不正確的登錄名,然後點擊進入彈出窗口,則關閉它並再次提交日誌。有沒有辦法阻止這種情況?所以當用戶按下回車鍵關閉彈出窗口時,輸入按鍵不會以我的登錄形式註冊?點擊輸入對話框無法正常工作

繼承我的登錄表單中的代碼。 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(); 
     } 
    } 
+0

我不是很確定你說的是發生了什麼,但我覺得你的問題在於,在'Form.AcceptButton'指定按鈕的事實您的表單是您按Enter鍵時按下的按鈕。所以如果你不想要這種行爲,你需要清除「Form.AcceptButton」的值。 – Pete

+0

此外,你不會得到一個'KeyUp'爲回車鍵,如果你擁有的AcceptButton集,但如果我沒有記錯,你會得到一個'KeyDown'它。 – Pete

回答

1

這是因爲您使用的KeyUp事件。並按下Enter鍵關閉消息框。這是正確的,它使用KeyDown事件。所以對話框關閉,TextBox獲取焦點並釋放Enter鍵。再次觸發KeyUp事件:)

還打開揚聲器聽到討厭的丁!您從不喜歡Enter鍵的文本框中獲得。

使用KeyDown事件來代替:

private void textBox2_KeyDown(object sender, KeyEventArgs e) { 
     if (e.KeyCode == Keys.Enter) { 
      button1.PerformClick(); 
      e.Handled = e.SuppressKeyPress = true; 
     } 
    } 
+0

有些東西出現了,所以我當時沒有嘗試這些答案。我只是試過你的,而且效果很好。謝謝! – Nathan

0

當這個問題發生時,重點是在textBox2表示的文本框?這可能是發生了什麼事情:

  1. 用戶輸入的用戶名和密碼不正確。
  2. 用戶在密碼字段中按下回車鍵並輸入焦點。
  3. 將顯示一個MessageBox,指示用戶名和密碼不正確。
  4. 用戶按下Enter鍵關閉MessageBox。

我不是這種積極的,但最有可能當接收到的輸入鍵的鍵按下事件的消息框關閉。焦點變回登錄表單,特別是密碼字段(焦點在顯示MessageBox之前)。關鍵事件發送給焦點控制,即密碼字段 - 它會再次觸發登錄。

要解決此問題,請移除鍵控處理程序並將Form.AcceptButton屬性設置爲「確定/登錄」按鈕,並將Form.CancelButton屬性設置爲「取消」按鈕。這將自動允許輸入按OK /登錄按鈕,然後按Esc退出按鈕。

+0

我幾乎是certian KeyDown和KeyUp事件不會去不同的窗口。也就是說,您不會在一個窗口中獲得KeyDown,而在另一個窗口中獲取關聯的KeyUp消息。 – Pete

0

您可以如下解決它。您還必須將KeyPreview屬性設置爲true。

獲取或設置指示事件被傳遞給具有焦點的控制之前的形式是否將接收鍵事件 的值。

 private void Form1_Load(object sender, EventArgs e) 
     { 
      this.KeyPreview = true; 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      if (string.IsNullOrEmpty(textBox1.Text.Trim()) || string.IsNullOrEmpty(textBox2.Text.Trim())) 
      { 
       MessageBox.Show("Please enter a username and password!"); 
      } 
      else if (IsValidUser(textBox1.Text.Trim(), textBox2.Text.Trim())) 
      { 
       MessageBox.Show("Succesfully logged in!"); 
       Program.ActiveUser = textBox1.Text; 
       this.Close(); 
      } 
      else 
      { 
       MessageBox.Show("Incorrect username or password!"); 
      } 
     } 
     private void Form1_KeyDown(object sender, KeyEventArgs e) 
     { 
      if (e.KeyCode == Keys.Enter) 
      { 
       button1.PerformClick(); 
      } 
     } 

     private bool IsValidUser(string userName, string userPassword) 
     { 
      for (int i = 0; i <= Program.ProfileList.Count - 1; i++) 
      { 
       if (Program.ProfileList[i].Username == userName) 
       { 
        AESEncrypt encrypt = new AESEncrypt(); 
        if (Program.ProfileList[i].Password == encrypt.EncryptToString(userPassword)) 
        { 
         return true; 
        } 
        else 
        { 
         return false; 
        } 
       } 
      } 
      return false; 
     } 
相關問題