我想用一个进程条来表示一个虚假的进程,只是想在一个长时间动作开始后,有一个显示给客户看,当动作完成后,进程条退出,表示动作完成。
我用Timer来控制进程条,发现加入一个长时间的动作代码后,并不能得到效果,总是在动作代码运行完后,直接就报出了结束的消息,不能得到想要的进度
的显示请教,,应该如何实现这个功能。。谢谢
本人代码如下
Dim i As Integer
Dim stopT As BooleanPrivate Sub Form_Load()
stopT = False
i = 0
bar.Max = 50
bar.Min = 0
Timer1.Enabled = True
Call doit
End SubPrivate Sub Timer1_Timer()
  bar.Value = i
  i = i + 1  If i = 50 Then
  i = 0
  End If
  If stopT = True Then
    Call stopBar
  End If
End SubPrivate Sub stopBar()
      Timer1.Enabled = False
      MsgBox "over"
End Sub
Private Sub doit()
       Dim count As Long
       For count = 0 To 10000000
       count = count + 1
       Next count
       stopT = True
End Sub

解决方案 »

  1.   

    For count = 0 To 10000000
           count = count + 1
           doevents
    Next count
      

  2.   

    Dim stopT As BooleanPrivate Sub Form_Load()
        stopT = False
        bar.Max = 50
        bar.Min = 0
        Timer1.Enabled = True
        Call doit
    End SubPrivate Sub Timer1_Timer()  If bar.Value = bar.Max Then
          bar.Value = 0
      Else
          bar.Value = bar.Value + 1
      End If
      bar.Refresh '强制刷新
      If stopT = True Then
        bar.Value = bar.Max
        Call stopBar
      End If
    End SubPrivate Sub stopBar()
          Timer1.Enabled = False
          MsgBox "over"
    End SubPrivate Sub doit()
       Dim count As Long
       For count = 0 To 10000000
           count = count + 1
       Next count
       stopT = True
    End Sub注意,编译成 .exe 文件后执行,才能使 Timer 并发。
      

  3.   

    长循环中间要加doevents,这样timer就有空执行了。
      

  4.   

    我试过大家的做法了,好象还是不行啊。。都是在doit()长动作运行完后,才跳出了bar的显示。。???是我做错了么???请指正。。
      

  5.   

    要用doevents语句空闲CPU时间,否则CPU没有时间重绘进度条。如果你的操作是单一的进程,比如执行一条SQL语句,这样你无法使用DOEVENTS语句,需要用线程,看你执行什么操作了。
      

  6.   

    Private Sub Timer1_Timer ()
        ProgressBar1.Value = ProgressBar1.Value + 10
        If ProgressBar1 >= 1000 Then
            ProgressBar1.Enabled =Flase
            Timer1.Enabled = False
        End If
    End Sub
      

  7.   

    可能与执行顺序有关,加在COMMBUTTON事件中情况要好点.
      

  8.   

    我个人不喜欢用TIMER 和 PROGRESSBAR,一般用两个PICTUREBOX代替Private Sub Form_Activate()
    doit
    End SubPrivate Sub Form_Initialize()Picture1.Move 0, 0, Me.Width, Me.Height / 5
    Set Picture2.Container = Picture1
    Picture2.AutoRedraw = True
    'picture2.picture=loadpicture("c:\123.jpg") '用图片效果更好
    Picture1.ScaleWidth = 1000000
    Picture2.Move 0, 0, 0, Picture1.Height
    Picture2.BackColor = vbRed '
    End SubPrivate Sub doit()
           Dim i As Long, max As Integer
           For i = 0 To 1000000
            DoEvents
           Picture2.Width = i
           Next
          MsgBox "over"
    End Sub