1

我正在学习 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
4

2 回答 2

1

要重写您尝试做的事情:
1. 使用 String.IsNullOrEmpty 测试文本框是否为空
2. 使用 Focus() 而不是 Select()
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
于 2013-09-30T10:37:03.040 回答
0

除了编码风格之外,通常您不会将密码作为明文存储在源代码中(反编译会显示它)。一种常见的方法是预先对密码进行哈希处理并将哈希存储在源代码中。这将使对密码进行逆向工程变得更加困难。

此外,您不应提供有关登录凭据有什么问题的准确信息,但还要说

提供的用户名/密码不匹配或相似。

对于多个用户,您应该使用

Dictionary(Of String, String)

和相关的方法,而不是无穷无尽的 if 子句。

于 2013-09-30T10:46:40.317 回答