我有这个项目,我正在尝试用最短的代码创建一个Login Attempt / s,所以我尝试使用循环。这是编码
Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
Dim attempts = 3
Do
If txtUsername.Text = username And txtPassword.Text = password Then
MessageBox.Show("ACCESS GRANTED")
Me.Hide()
MainMenu.Show()
Else
MessageBox.Show("ACCESS DENIED")
txtUsername.Text = "input username"
txtPassword.Text = "input password"
attempts = attempts - 1
End If
Loop Until attempts <= 0
MessageBox.Show("Maximum Login Attempts Reached!")
End Sub
但似乎我处于悖论或无限循环中。我正在考虑像继续一样控制循环;并打破;像java那样..有什么方法可以在visual basic中做到这一点?
正如汉斯指出的那样,你需要将attempt
移到方法之外。只需在表单Class中创建变量,然后使用每个onClick事件进行尝试。希望这是有道理的,下面的代码应该给你想要的结果!
Dim Attempts As Integer = 0
Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
Attempts += 1
If Attempts > 3 Then
MessageBox.Show("Maximum Login Attempts Reached!")
Exit Sub
'User has tried too many times - just keep exiting the method, or you can use
'Application.Exit()
'This will just quit the program..
End If
If txtUsername.Text = username And txtPassword.Text = password Then
MessageBox.Show("ACCESS GRANTED")
Me.Hide()
MainMenu.Show()
Else
MessageBox.Show("ACCESS DENIED")
txtUsername.Text = "input username"
txtPassword.Text = "input password"
End If
End Sub
如果你想让应用程序退出,那么取消对Application.Exit
的注意,并注释掉Exit Sub
,如果你想重置尝试(无论出于什么原因)你可以在调用Attempts = 0
时添加
Hth Chicken