ProgressBar Control Scenario 2: Using the ProgressBar to Show a TimeOut Interval

Applications that create processes over networks often have a "TimeOut" interval. This is a predetermined period of time after which the user will be presented with the choice of canceling a process, or continuing to wait. One way of graphically representing the TimeOut interval is with the ProgressBar control.

The following example uses the following objects:

To create a progress bar that reflects a TimeOut interval

  1. In the Form's Load event, set a Timer control's Interval property to 1000.

  2. Set the ProgressBar control's Max property to the TimeOut Interval.

  3. Begin the Timer with the Enabled property.

  4. In the Timer control's Timer event, declare a Static variable to count the number of intervals.

  5. Set the ProgressBar's value to the variable.

  6. Test to see if the ProgressBar's value is the Max property.

In the Form's Load event, Set the Timer Control's Interval Property to 1000

In the Form object's Load event, configure the Timer control's Interval property. Because it's more useful to time a process in seconds, set the Interval to 1000 (milliseconds, or 1 second). Thus, at one second intervals, the ProgressBar control's Value property is updated.

tmrTimer.Interval = 1000

In the Form Load event, Set the ProgressBar Control's Max Property to the TimeOut Interval

The Load event is also where you set the Max property of the ProgressBar. The value of the Max property should be the number of seconds you want the Timer to continue before being disabled. However, to accurately reflect the number of seconds that must elapse, the ProgressBar's Min property should be set to 1.

The Load event can also be used to hide the ProgressBar by setting its Visible property to False. The following code shows the entire Load event with the previous code included.

Private Sub Form_Load()
   prgBar1.Visible = False
   tmrTimer.Interval = 1000
   prgBar1.Max = 10 ' Timer will go for 10 seconds.
End Sub

Begin the Timer with the Enabled Property

To start the timer, you must use the Enabled property. When you begin to time any process, you should also show the ProgressBar, as shown:

Private Sub cmdBegin_Click()
   prgBar1.Visible = True
   tmrTimer.Enabled = True
End Sub

In the Timer Event, Declare a Static Variable and Set it to 1

In the Timer event, declare a static variable. This allows you to efficiently increment the variable every time the Timer event occurs. But as we don't wish to count from 0, we must also set the variable to 1, using the IsEmpty function, as shown:

Static intTime
If IsEmpty(intTime) Then intTime = 1

Set the ProgressBar's Value to the Variable

Each time the Timer event occurs, the ProgressBar's Value property must be set to the value of the static variable:

prgBar1.Value = intTime

Test to See If the ProgressBar's Value is the Max Property.

After the ProgressBar's Value property has been updated, the variable must be tested to see if the TimeOut limit has occurred. If it has been reached, the variable must be reset to 1, the ProgressBar control hidden and its Value property reset to 1, and the Timer control disabled. If the limit hasn't been reached, then the variable is incremented by one. These steps are all implemented with an If statement, in the Timer event, as shown:

Private Sub tmrTimer_Timer()
   Static intTime ' Declare the static variable.
   ' The first time, the variable will be empty. 
   ' Set it to 1 if it is an empty variable.
   If IsEmpty(intTime) Then intTime = 1
   
   prgBar1.Value = intTime ' Update the ProgressBar.
       
   If intTime = prgBar1.Max Then
      Timer1.Enabled = False
      prgBar1.Visible = False
      intTime = 1
      prgBar1.Value = prgBar1.Min   
   Else
      intTime = intTime + 1
   End If
End Sub

The Complete Code

Here is the complete code for the example described in this topic:

Private Sub Form_Load()
   prgBar1.Visible = False
   tmrTimer.Interval = 1000
   prgBar1.Max = 10 ' Timer will go for 10 seconds.
End Sub

Private Sub cmdBegin_Click()
   prgBar1.Visible = True
   tmrTimer.Enabled = True
End Sub

Private Sub tmrTimer_Timer()
   Static intTime ' Declare the static variable.
   ' The first time, the variable will be empty. 
   ' Set it to 1 if it is an empty variable.
   If IsEmpty(intTime) Then intTime = 1
   
   prgBar1.Value = intTime ' Update the ProgressBar.
       
   If intTime = prgBar1.Max Then
      Timer1.Enabled = False
      prgBar1.Visible = False
      intTime = 1
      prgBar1.Value = prgBar1.Min   
   Else
      intTime = intTime + 1
   End If
End Sub