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. Select() の代わりに Focus() を使用
します。 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 に答える