Visual Basic Concepts

ProgressBar Scenario 1: Inform User of TreeView Population Status

See Also

If you are using a TreeView control to view a database, populating the tree may take some time. In that instance, you can use the ProgressBar control to inform the user of the status of the operation.

In this scenario, the Biblio.mdb database is loaded into the TreeView control using a Do Until loop. Before the loop begins the ProgressBar control is unhidden. Through each iteration of the loop, the Value property of the ProgressBar control is updated with the PercentPosition property of the Recordset object. When the loop finishes, the ProgressBar control is hidden again.

Sample Application: DataTree.vbp

The code examples in this topic are taken from the DataTree.vbp sample application, which is listed in the Samples directory.

Objects Used

The following example uses the following objects:

To show a ProgressBar while a TreeView control is being populated

  1. In the Form object's Load event. hide the ProgressBar and set its Max property to 100.
  2. In the TreeView population code, populate the tree with a Do Until statement.

  3. When the loop ends, hide the ProgressBar.

In the Form Object's Load Event, Hide the ProgressBar and Set the Max Property to 100

A progress bar is most effective when it is visible only for the duration of the time-intensive process. To this end, you can use the Form object's Load event to set the Visible property of the control to False.

In this scenario, the Recordset object's PercentPosition property will be used. Since this property returns a number representing a percentage between 0 and 100, the Max property should be set to 100 when the form is initialized. (By default, the Min property is set to 0.) The following code hides the ProgressBar control and sets its Max property to 100.

Private Sub Form_Load()
   prgLoad.Visible = False
   prgLoad.Max = 100
End Sub

Populate the Tree with a Do Until Statement

Note   The following code builds upon the code found in "TreeView Scenario1: Binding the TreeView to the Biblio.MDB Database." The working code can also be found in the DataTree.vbp sample application.

To populate a TreeView control from a database, you can use a Do Until loop. Schematically, the code might look like this:

' Presuming a Recordset object variable named 
' "rsTitles" has been set to a valid database 
' table.
Do Until rsTitles.EOF
   ' Using the current record, create a Node
   ' object.
   Set mNode = TreeView.Nodes.Add()
   ' Set properties of the Node.
   mNode.Text = rsTitles!Fields(1).Value
   ' Move to the next record.
   rsTitles.MoveNext
Loop 

To update the ProgressBar control, use the PercentPosition property of the Recordset object. This property returns the position of the current recordset as a percentage of the total number of records. The code to update the ProgressBar should then be placed somewhere within the loop, as shown:

Do Until rsTitles.EOF
   ' Update the ProgressBar control.
   prgLoad.Value = rsTitles.PercentPosition

   Set mNode = TreeView.Nodes.Add()
   ' Set properties of the Node.
   mNode.Text = rsTitles!Fields(1).Value
   ' Move to the next record.
   rsTitles.MoveNext
Loop

When the Loop Ends, Hide the ProgressBar

Once the TreeView control has been entirely loaded with Node objects, you can hide the ProgressBar control, as shown:

Private Sub cmdLoad_Click()
   ' Show the ProgressBar control.
   prgLoad.Visible = True

Do Until rsTitles.EOF
      ' Update the ProgressBar control.
      prgLoad.Value = rsTitles.PercentPosition

      Set mNode = TreeView.Nodes.Add()
      ' Set properties of the Node.
      mNode.Text = rsTitles!Fields(1).Value
      ' Move to the next record.
      rsTitles.MoveNext
   Loop
   ' Hide the ProgressBar control.
   prgLoad.Visible = False
End Sub