How to show the progress bar programmatically


The ShowProgress macro demonstrates how to programmatically show a process in status bar in ArcMap. It uses USA States shapefile and sums the field of "POP1990". When each record is read and calculated, the statusbar shows the process infomation in the message pane and progress bar. In order to have the process slow enough to see the progress bar move, this sample pauses time explicitly.

How to use

  1. Add USA States shapefile in ArcMap.
  2. Paste this code into a module in the Visual Basic Editor in ArcMap.
  3. Run the ShowProgress macro.
[VBA]
Sub ShowProgress()
    Dim pDoc As IMxDocument
    Dim pMap As IMap
    Dim pLayer As ILayer
    Dim pFeatLayer As IFeatureLayer
    Dim pFeatCursor As IFeatureCursor
    Dim pFeatClass As IFeatureClass
    Dim pFeature As IFeature
    Dim dSum As Double
    Dim lFieldIndex As Long
    Dim lNumFeat As Long
    Dim dInterval As Double
    
    Set pDoc = Application.Document
    Set pMap = pDoc.FocusMap
    Set pLayer = pMap.Layer(0)
    Set pFeatLayer = pLayer
    Set pFeatClass = pFeatLayer.FeatureClass
    Set pFeatCursor = pFeatLayer.Search(Nothing, True)
    
    Dim psbar As IStatusBar
    Set psbar = StatusBar
    Dim pPro As IStepProgressor
    Set pPro = psbar.ProgressBar
    
    lNumFeat = pFeatClass.FeatureCount(Nothing)
    dInterval = lNumFeat / 100
    Set pFeature = pFeatCursor.NextFeature
    lFieldIndex = pFeature.Fields.FindField("POP1990")
    
    Dim PauseTime, Start, Finish, TotalTime, i
    PauseTime = 0.5
    pPro.MinRange = 1
    pPro.MaxRange = lNumFeat
    pPro.StepValue = dInterval
    
    For i = 1 To lNumFeat
        dSum = dSum + pFeature.Value(lFieldIndex)
        Set pFeature = Nothing
        Set pFeature = pFeatCursor.NextFeature
        
        pPro.Position = i
        pPro.Message = "Reading record " & Str(i) & ". Sum =" & Str(dSum)
        pPro.Step
        pPro.Show
        Start = Timer
        Do While Timer < Start + PauseTime
            DoEvents
        Loop
    Next
    pPro.Hide
End Sub