我想在登录功能中添加登录尝试次数。当用户输入错误的用户名和密码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次登录尝试失败?
答案 0 :(得分: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
答案 1 :(得分:0)
我将cnt添加为整数并将其递增直到cnt <= 3。
Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)Handles 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`
答案 2 :(得分: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
HTH
答案 3 :(得分: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
答案 4 :(得分: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
结束班级