Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse a ProgressBar to give the status of a long task
Keywordsprogress bar, ProgressBar, long task, status
CategoriesSoftware Engineering, Controls
 
When beginning a long task, make the ProgressBar visible. Set its Min and Max properties to represent the task. Enable a timer and begin the long task.

The task periodically calls DoEvents to let the Timer fire. The Timer updates the ProgressBar. Note that the program disables its button while the task is running so the user cannot click it again.

 
Private Sub cmdPerformTask_Click()
Dim j As Long

    ' Don't let the user click this till we
    ' are done with this task.
    cmdPerformTask.Enabled = False
    MousePointer = vbHourglass

    ' Start the progress bar at zero.
    pbTaskProgress.Value = pbTaskProgress.Min
    pbTaskProgress.Visible = True

    ' Enable the timer to give status reports.
    tmrTaskTimer.Enabled = True

    ' Perform the long task.
    For m_PercentDone = 1 To 100
        ' Do something long.
        For j = 1 To 500000
        Next j

        ' Give the timer a chance.
        DoEvents
    Next m_PercentDone

    ' Disable the timer and reenable the button.
    tmrTaskTimer.Enabled = False
    cmdPerformTask.Enabled = True
    MousePointer = vbDefault
    pbTaskProgress.Visible = False
End Sub

Private Sub tmrTaskTimer_Timer()
    ' Increase the ProgressBar's value.
    pbTaskProgress.Value = m_PercentDone
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated