我學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
感謝回答,所以對於「PerformClick()」你說要保持,作爲自己的「按回車鍵查詢」子,而不是保持在txtPassword子的? –
是的,避免在另一個事件中調用事件。所以,把所有的邏輯在'PerformClick method'然後調用它在你的'btnLogin_Click()'和'txtPassword_KeyDown()' –
真棒!哈哈謝謝!! –