如何计算登录尝试 Visual Basic [英] How to count login attempts Visual Basic
问题描述
我想为我的登录功能添加登录尝试次数.当用户输入错误的用户名和密码 3 次时,程序应关闭并显示一条消息.这是我的 Form1.vb
中登录按钮的代码:
I want to add a count of login attempts to my login function. When a user types a wrong username and password 3 times, the program should close down and show a message. Here is my code for the Login button in my 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 次登录尝试失败?
What can I do to add a count into this code to properly notify the user of their 3 failed login attempts?
推荐答案
正如一些人所建议的,您可以创建一个变量 (cntAttempts
) 来跟踪用户尝试登录的次数,如果计数达到 3,则程序关闭.像这样:
As several people have suggested, you could create a variable (cntAttempts
) that keeps track of how many times the user tries to login, and if the count reaches 3, the program closes. Like this:
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
这篇关于如何计算登录尝试 Visual Basic的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!