我正在设计Visual Basics 6.0的登录页面。我必须包含一个计数器,以便在三次失败的尝试后页面将自行关闭。我在命令按钮中编写了以下代码。虽然没有错误,但计数器不起作用。这是代码:
Private Sub Command1_Click()
Dim count As Integer
count = 0
If Form1.Text1 = "admin" And Form1.Text2 = "admin" Then
MsgBox "Login Succesfull", vbOKOnly + vbInformation, "Welcome"
Else
count = count + 1
If count = 3 Then
End
Else
MsgBox "Login Unsuccesfull", vbOKOnly + vbCritical, "Try Again"
Form1.Text1 = ""
Form1.Text2 = ""
Form1.Text1.SetFocus
End If
End If
End Sub
只需将"暗"更改为"静态",使其在调用中保持其值。 并删除将其设置为零的行:
Private Sub Command1_Click()
Static count As Integer
If Form1.Text1 = "admin" And Form1.Text2 = "admin" Then
MsgBox "Login Succesfull", vbOKOnly + vbInformation, "Welcome"
Else
count = count + 1
If count = 3 Then
End
Else
MsgBox "Login Unsuccesfull", vbOKOnly + vbCritical, "Try Again"
Form1.Text1 = ""
Form1.Text2 = ""
Form1.Text1.SetFocus
End If
End If
End Sub
每次单击 Command1 时,都将计数设置为 0。您需要在该 sub 之外声明count
,并在其他地方将其初始化为 0。
Private count As Integer
Private Sub Command1_Click()
If Form1.Text1 = "admin" And Form1.Text2 = "admin" Then
MsgBox "Login Succesfull", vbOKOnly + vbInformation, "Welcome"
Else
count = count + 1
If count = 3 Then
End
Else
MsgBox "Login Unsuccesfull", vbOKOnly + vbCritical, "Try Again"
Form1.Text1 = ""
Form1.Text2 = ""
Form1.Text1.SetFocus
End If
End If
End Sub