2013-09-30 66 views
1

我學VB,並已取得我已搜查谷歌的點點滴滴,並通過我的教科書「登錄」框。 我只是想讓你們看看,並告訴我,如果它是好的代碼或不...VB.Net登錄框測試

我測試了它,它的工作..所以我知道這很多看起來'專業'或狡猾?

Public Class mainLogin 
    Private Sub mainLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
     ' selects the username box when form loads 
     txtUsername.Select() 
    End Sub 

    Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click 
     If txtUsername.Text = "" Then 
      MessageBox.Show("Username field is empty.") 
      txtUsername.Select() 
      Exit Sub 
     End If 

     If txtPassword.Text = "" Then 
      MessageBox.Show("Password field is empty.") 
      txtPassword.Select() 
      Exit Sub 
     End If 

     If txtPassword.Text.Length < 8 Then 
      MessageBox.Show("Password length must be more then 8 characters.") 
      txtPassword.Clear() 
      Exit Sub 
     End If 

     If txtUsername.Text = "PavleS" Then 
      If txtPassword.Text = "Password11" Then 
       MessageBox.Show("Success!") 

       ' Do something fancy here.. 
      Else 
       MessageBox.Show("Bad Password!") 
      End If 
     Else 
      MessageBox.Show("Bad Username!") 
     End If 
    End Sub 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
     ' clears username and password fields 
     txtPassword.Text = "" 
     txtUsername.Text = "" 
    End Sub 

    Private Sub txtPassword_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtPassword.KeyDown 
     If e.KeyCode = Keys.Enter Then 
      ' If Enter on the keyboard is pressed it will preform 
      ' the same action as clicking the login button 
      btnLogin.PerformClick() 
     End If 
    End Sub 
End Class 

回答

1

要重寫你已經嘗試做的:
1.使用String.IsNullOrEmpty測試如果文本框爲空
2.使用對焦(),而不是選擇()
3.避免調用來自另一個的事件。如果2個事件都做同樣的事情,將所有的邏輯在私有方法,並呼籲從兩個事件的方法。(參考你的代碼txtPassword.KeyDown())。

Private Sub mainLogin_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown 

    ' selects the username box when form loads 
    txtUsername.Focus() 

End Sub 


Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click 


    If String.IsNullOrEmpty(txtUsername.Text) Then 
     MessageBox.Show("Username field is empty.") 
     txtUsername.Focus() 
     Exit Sub 
    End If 

    If String.IsNullOrEmpty(txtPassword.Text) Then 
     MessageBox.Show("Password field is empty.") 
     txtPassword.Focus() 
     Exit Sub 

    Else If txtPassword.Text.Length < 8 Then 
     MessageBox.Show("Password length must be more then 8 characters.") 
     txtPassword.Clear() 
     Exit Sub 
    End If 


    If txtUsername.Text = "PavleS" Then 

     If txtPassword.Text = "Password11" Then 
      MessageBox.Show("Success!") 

      ' 
      ' Do something fancy here.. 
      ' 
     Else 
      MessageBox.Show("Bad Password!") 
     End If 

    Else 
     MessageBox.Show("Bad Username!") 
    End If 

End Sub 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 

    ' clears username and password fields 
    txtPassword.Clear() 
    txtUsername.Clear() 

End Sub 


Private Sub txtPassword_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) _ 
    Handles txtPassword.KeyDown 

    If e.KeyCode = Keys.Enter Then 

     ' 
     ' If Enter on the keyboard is pressed it will preform 
     ' the same action as clicking the login button 
     ' 
     PerformClick() 

    End If 

End Sub 

    Private Sub PerformClick() 
     '' Perform your logic here 
    End Sub 
+0

感謝回答,所以對於「PerformClick()」你說要保持,作爲自己的「按回車鍵查詢」子,而不是保持在txtPassword子的? –

+0

是的,避免在另一個事件中調用事件。所以,把所有的邏輯在'PerformClick method'然後調用它在你的'btnLogin_Click()'和'txtPassword_KeyDown()' –

+0

真棒!哈哈謝謝!! –

0

除了編碼風格,通常你不會在源代碼中存儲密碼以明文(decompliation將揭示它)。一個常見的做法是哈希密碼事先和散列存儲在源代碼。這將使密碼反向工程變得更加困難。

而且,你不應該放棄對什麼是錯誤的登錄憑證準確的信息,但另外說

提供的用戶名/密碼不匹配 或相似。

對於多個用戶,你應該使用

Dictionary(Of String, String) 

和相關方法,,而不是無休止的if子句。

+0

是的,所以'keepem'猜測哪一個是錯誤的LOL .... 我最終將連接到一個數據庫使用SQL ...我想...還不知道 –