插入了我的进度条,但没有按预期工作。当我登录时,它可以工作,但它不会自动加载第二个表单。
这是我的进度/计时器代码
Private Sub Timer1_Timer()
If ProgressBar1.Value = 100 Then
ProgressBar1.Value = 0
Else
ProgressBar1.Value = Val(ProgressBar1.Value) + Val(20)
End If
Label3.Caption = ProgressBar1.Value
标签,检测100,变量prg保持
Private Sub Label3_Change()
If (Label3.Caption = 100) Then
prg = 1
Timer1.Interval = 0
End If
End Sub
登录按钮
Private Sub cmdSign_Click()
currentTime = TimeValue(Now)
strCurrdate = Format(Date, "mmm d, YYYY")
rx.Open "Select * from login where username ='" & Text1 & "' and password='" & Text2 & "'", db, 3, 3
If (counter = 3) And (rx.EOF = True) Then
MsgBox "You guessed too many times! Intruder alert!"
End
Else
If rx.EOF = True Then
MsgBox "Invalid Username or Password"
counter = counter + 1
Text1 = ""
Text2 = ""
Else
user1 = Text1.Text
logTime = currentTime
rxd.Open "Select * from logHistory", db, 3, 3
With rxd
.AddNew
.Fields("username") = user1
.Fields("TimeDate") = strCurrdate
.Fields("TimeIn") = logTime
.Update
End With
Set rxd = Nothing
'problem might be here
ProgressBar1.Visible = True
Timer1.Interval = 100
Label3.Visible = True
Timer1.Enabled = True
If prg = 1 Then
MsgBox "Welcome " + Text1 + " to SPARTAN!"
mainmenu.Show
End If
End If
End If
Set rx = Nothing
End Sub
对此有何帮助?
答案 0 :(得分:1)
启动计时器后,它将与您的其余代码异步运行。您编写登录按钮的方式是期望计时器将进度条从0移动到100,将您的(我假设)模块作用域变量 prg 设置为1,然后继续。实际上,您的代码启用了计时器并继续运行。当它到达If prg = 1 Then
语句时,prg仍然是它已被初始化的任何内容。修复它的一种方法是检查计时器事件中的prg变量。
Private Sub Timer1_Timer()
If ProgressBar1.Value = 100 Then
Timer1.Enabled = False ' don't fire the timer event again
ProgressBar1.Value = 0
MsgBox "Welcome " & Text1 & " to SPARTAN!"
mainmenu.Show
Else
ProgressBar1.Value = Val(ProgressBar1.Value) + Val(20)
End If
Label3.Caption = ProgressBar1.Value
End Sub
我还将字符串连接 + 更改为& 。加号“+”有效,但为了向后兼容性,仅在3之后的VB版本中包含,并且除了算术之外,它被认为是不好的形式。在VB版本4及更高版本中&符号“&”应该用于连接。 MSDN reference here