我有一个ProgressBar和一个计时器,ProgressBar的值增量取决于计时器。 当我达到ProgressBar的最大值时,我想显示一条消息,所以我有以下代码:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If ProgressBar1.Value = ProgressBar1.Maximum Then
Timer1.Enabled = False
MessageBox.Show("It’s over now !")
Else
ProgressBar1.Value = ProgressBar.Value + 1
End If
End Sub
但是它不起作用,我只显示一千次。我该如何解决?谢谢
答案 0 :(得分:0)
您可以尝试使用此代码
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
ProgressBar1.Value = ProgressBar1.Value + 1
If ProgressBar1.Value = 100 Then
Timer1.Stop()
MsgBox("Its reached")
ProgressBar1.Value = 0
Exit Sub
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Timer1.Stop()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Timer1.Start()
End Sub
答案 1 :(得分:0)
消息框无限显示的原因是,对于每个计时器刻度,您都在检查进度条是否处于最大值。如果是,则创建一个消息框。一旦达到最大值,您实际上就是在每个刻度上创建消息框。
最简单的解决方法是在达到这一点后立即停止计时器,您可以通过编辑代码来做到这一点:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If ProgressBar1.Value = ProgressBar1.Maximum Then
Timer1.Stop() 'Edit is here <--
MessageBox.Show("It’s over now !")
Else
ProgressBar1.Value += 1 '<-- I have also corrected this statement
End If
End Sub
更新:将Timer1.Stop()
移至消息框上方
答案 2 :(得分:-5)
尝试一下:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
ProgressBar1.Value += 1
If ProgressBar1.Value < ProgressBar1.Maximum Then Exit Sub
Timer1.Enabled = False
ProgressBar1.Value = ProgressBar1.Maximum
MessageBox.Show("It's over now!")
End Sub
值为3728(具有单个整数增量)对于Timer
可能会花费很长时间,但是,我确定您已经意识到这一点。