vb.net Visual Basic 登录表单查询(3 次尝试后关闭程序)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26166801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Visual basic login form query (Close program after 3 attempts)
提问by Finn
I am trying to create a login form, I have predefined the password.
我正在尝试创建一个登录表单,我已经预定义了密码。
However, I want to only allow the user 3 attempts at logging in, if they fail it will say, you have used your three attempts and the program will close, here is my code so far:
但是,我只想允许用户 3 次尝试登录,如果他们失败,它会说,您已经使用了 3 次尝试并且程序将关闭,这是我目前的代码:
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
Dim Password As String
Dim x As String
Dim Attempts As Integer
'maybe use for loop
Password = "House"
x = txtPassInput.Text
If Attempts = 3 Then
Me.Close()
End If
If x = Password Then
MsgBox("Correct password, you are now logged in.")
Else
Attempts = Attempts + 1
MsgBox("You have entered the wrong password.")
End If
End Sub
What I am asking is, how do I get it to close after 3 attempts, my program doesn't do this yet.
我要问的是,我如何在 3 次尝试后关闭它,我的程序还没有这样做。
回答by
Dim LoginAttempts As Integer
Dim currentPassword As String = "House"
Private Sub Login(ByVal passwordFromTextBox As String)
If Not TestLoginForCompare(passwordFromTextBox) Then
LoginAttempts += 1
If LoginAttempts = 3 Then
'close form/program, return error, lock account
else
'notify user of failed attempt
End If
Else
LoginAttempts = 0
'proceed to success login
End If
End Sub
Private Function TestLoginForCompare(ByVal password As String) As Boolean
If String.Compare(currentPassword, password) Then
Return True
Else
Return False
End If
End Function

