我正在学习 VB,并制作了一个“登录”框,我在谷歌上搜索了一些零碎的东西,并通过我的教科书。我只是想让你们看看并告诉我它是否是好的代码......
我已经对其进行了测试并且它有效..所以我知道它看起来很“专业”还是狡猾?
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