0

我想在我的登录功能中添加登录尝试次数。当用户输入错误的用户名和密码 3 次时,程序应关闭并显示一条消息。这是我的登录按钮的代码Form1.vb

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If TextBox1.Text = "13Mendv" And TextBox2.Text = "Admin123" Or
       TextBox1.Text = "Admin" And TextBox2.Text = "Admin123" Or
       TextBox1.Text = "13PateS" And TextBox2.Text = "Staff123" Or
       TextBox1.Text = "13KhetP" And TextBox2.Text = "Member123" Or
       TextBox1.Text = "13PateN" And TextBox2.Text = "Scorer123" Or
       TextBox1.Text = "13ChatP" And TextBox2.Text = "Captain123" Or
       TextBox1.Text = "13BonnN" And TextBox2.Text = "Captain123" Or
       TextBox1.Text = "13EarlJ" And TextBox2.Text = "Captain123" Or
       TextBox1.Text = "13RajaA" And TextBox2.Text = "Captain123" Or
       TextBox1.Text = "1" And TextBox2.Text = "1" Or
       TextBox1.Text = "13SchaJ" And TextBox2.Text = "Captain123" Then
        Timer1.Start() 'Timer on Form1.vb show
        ProgressBar1.Show() 'Progress bar on Form1.vb show
        Label8.Show() 'Label8 on Form1.vb show
        Button4.Show() 'Button4 on Form1.vb show

    Else
        If TextBox1.Text = "" And TextBox2.Text = "" Then
            MsgBox("No Username and/or Password Found!", MsgBoxStyle.Critical, "Error") 'If statement for checking if there is any input in either username or password entry field
        Else
            If TextBox1.Text = "" Then
                MsgBox("No Username Found!", MsgBoxStyle.Critical, "Error") 'Message box no username found
            Else
                If TextBox2.Text = "" Then
                    MsgBox("No Password Found!", MsgBoxStyle.Critical, "Error") 'Message box no password found
                Else
                    MsgBox("Invalid Username And/Or Password!", MsgBoxStyle.Critical, "Error") 'Message box invlaid username and or password
                    TextBox2.Clear()
                End If
            End If
        End If
    End If

End Sub

我该怎么做才能在此代码中添加计数以正确通知用户他们 3 次失败的登录尝试?

4

5 回答 5

1
Public Class Form1    
    Dim attempts As Integer = 0

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim user As String
        user = TextBox1.Text
        If user = "Jhayboy" Then
            MsgBox("Access Granted")
            Form2.Show()
            Me.Hide()

        ElseIf attempts = 3 Then
            MsgBox("Maximum count of retries(3),And you'reach the maximum attempts!Try again later", MsgBoxStyle.Critical, "Warning")
            Close()
        Else
            MsgBox("Username and Password is incorrect! re-enter again you currently have reached attempt " & attempts & " of 3.")

            attempts = attempts + 1
            TextBox1.Text = ""
            TextBox1.Focus()

        End If
    End Sub
End Class
于 2016-03-26T05:50:57.403 回答
0

我将 cnt 添加为整数并递增它直到 cnt<=3 。

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

   'Dim cnt As Integer = 0
    If cnt <= 3 Then
        If TextBox1.Text = "13Mendv" And TextBox2.Text = "Admin123" Or
           TextBox1.Text = "Admin" And TextBox2.Text = "Admin123" Or
           TextBox1.Text = "13PateS" And TextBox2.Text = "Staff123" Or
           TextBox1.Text = "13KhetP" And TextBox2.Text = "Member123" Or
           TextBox1.Text = "13PateN" And TextBox2.Text = "Scorer123" Or
           TextBox1.Text = "13ChatP" And TextBox2.Text = "Captain123" Or
           TextBox1.Text = "13BonnN" And TextBox2.Text = "Captain123" Or
           TextBox1.Text = "13EarlJ" And TextBox2.Text = "Captain123" Or
           TextBox1.Text = "13RajaA" And TextBox2.Text = "Captain123" Or
           TextBox1.Text = "1" And TextBox2.Text = "1" Or
           TextBox1.Text = "13SchaJ" And TextBox2.Text = "Captain123" Then
            Timer1.Start() 'Timer on Form1.vb show
            ProgressBar1.Show() 'Progress bar on Form1.vb show
            Label8.Show() 'Label8 on Form1.vb show
            Button4.Show() 'Button4 on Form1.vb show
        Else
            cnt = cnt + 1
            If TextBox1.Text = "" And TextBox2.Text = "" Then
                MsgBox("No Username and/or Password Found!", MsgBoxStyle.Critical, "Error") 'If statement for checking if there is any input in either username or password entry field
            Else
                If TextBox1.Text = "" Then
                    MsgBox("No Username Found!", MsgBoxStyle.Critical, "Error") 'Message box no username found
                Else
                    If TextBox2.Text = "" Then
                        MsgBox("No Password Found!", MsgBoxStyle.Critical, "Error") 'Message box no password found
                    Else
                        MsgBox("Invalid Username And/Or Password!", MsgBoxStyle.Critical, "Error") 'Message box invlaid username and or password
                        TextBox2.Clear()
                    End If
                End If
            End If
        End If
    End If
End Sub`
于 2013-11-04T17:31:59.263 回答
0

正如一些人建议的那样,您可以创建一个变量 ( cntAttempts) 来跟踪用户尝试登录的次数,如果计数达到 3,则程序关闭。像这样:

Private cntAttempts = 0
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If ... Then
        Timer1.Start() 'Timer on Form1.vb show
        ProgressBar1.Show() 'Progress bar on Form1.vb show
        Label8.Show() 'Label8 on Form1.vb show
        Button4.Show() 'Button4 on Form1.vb show

    Else
        cntAttempts += 1
        If cntAttempts = 3 Then
            MessageBox.Show("login failed")
            Me.close()
        End If
        If TextBox1.Text = "" And TextBox2.Text = "" Then
            ...
        Else
            ...
        End If
    End If

End Sub

高温高压

于 2013-11-05T00:12:07.713 回答
0
    Dim a =0

    If txtname.Text = "yourUsername" And txtpass.Text = "yourPassword" Then
    Msgbox("Acces Granted")
    a=0
    Elseif txtname.Text <> "yourUsername" Or txtpass.Text <> "yourPassword" Then
    a = MsgBox("INVALID USERNAME AND PASSWORD") + a

    End if

    If 
    a = 3 Then
    End 

    End if
于 2014-08-28T11:54:28.290 回答
0

这可能是解决此问题的最佳方法:

 If TextBox1.Text = "Username" And TextBox2.Text = "Password" Or
       TextBox1.Text = "Admin" And TextBox2.Text = "Admin123" Then
        MsgBox("Welcome!")
        Me.Hide()
        Form1.Show()

    Else

        If TextBox1.Text = "" And TextBox2.Text = "" Then
            MsgBox("No Username and/or Password Found!", MsgBoxStyle.Critical, "Error") 
        Else
            If TextBox1.Text = "" Then
                MsgBox("No Username Found!", MsgBoxStyle.Critical, "Error")
            Else
                If TextBox2.Text = "" Then
                    MsgBox("No Password Found!", MsgBoxStyle.Critical, "Error") 
                Else
                    MsgBox("Invalid Username And/Or Password!", MsgBoxStyle.Critical, "Error")
                    TextBox2.Clear()
                End If
            End If
        End If
    End If
End Sub

结束类

于 2018-12-17T12:48:38.090 回答