The Progress Bar is one of the simplest controls in Visual Basic. The bar reports on the status of some computer operation, for example during the installation of new software or some other lengthy process such as a network file transfer. The bar gives the user some assurance that the program has not crashed. The process is the same in both Visual Basic 6.0 and Visual Basic 2008 Express Edition although the code in the latter is slightly different.
How It Works
As an operation proceeds, the code continuously reads how much is completed and calculates the status. If the Minimum value is set to one and the Maximum to 100, the bar will fill half the bar display when 50 percent of the operation is completed. To perform this calculation, the programmer must know how long the operation will take. As an example, if the user is downloading a five MB file, by the time 2.5 MB have downloaded, the bar will have moved halfway across.
Create a Progress Bar
Start by creating a new project after opening Visual Basic. Click on "File," "New Project" and select the "Standard EXE" format. Add the ProgressBar control by clicking on "Project," "Components." Find the Microsoft Windows Common Controls in the list. Choose the Microsoft Visual Basic Common Controls (SP6) by placing a checkmark in the box next to the control. Click on "Apply" and "OK." You should see the control in the list in the left panel. Double click on this to add it to the form.
Set the Properties
If working with an operation where the percentage of completion is easy to calculate like a file download, set the Minimum property in the Properties panel to "1" and the maximum to "100." Another way to set the maximum is to have the code count the number of iterations of some operation, compare this to the total number, and calculate where the bar should be. Height and Left properties control the height of the bar itself and where it appears from the left of the screen. Only two other properties matter: Whether the bar runs across the screen vertically or horizontally and whether incremental chunks appear or the bar is smoother. Choose these from the "Orientation" and "Scrolling Properties." If the maximum cannot be predetermined, it is possible to use an Animation control to keep the bar moving until the operation completes. This enhancement goes beyond the level of this tutorial. Since the bar should not appear until an operation commences, set the Visible property to "False." For this example, set the Maximum property to "10000" and leave the Minimum at "0." Finally, add a Command Button control from the Tools list to the form to initiate action. Change the (Name) property to "btnStart" and the Caption to "Start."
Write the Code
Click on "View," "Code" to open the programming window. Type the lines below exactly as shown. The first three lines load the form and make the Progress Bar visible. The rest of the code runs through a repetitive sequence to demonstrate the bar. The end number in this code is set very high to slow the Progress Bar down for visibility. Replace the steps in the second section beginning with "Private Sub btnStart_Click() and ending "End Sub" with whatever event you are timing to get a new ProgressBar1.Value. Always add a command to change the visibility of the bar to "False" when the routine finishes.
Private Sub Form_Load()
ProgressBar1.Visible = True
Private Sub btnStart_Click()
Dim n As Integer
For n = 1 To 10000
ProgressBar1.Value = n
Test the code by pressing the "F5" key.
Visual Basic 2008 Express Edition Version
In the Visual Basic 2008, substitute the following lines for the lines beginning Private Sub:
Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
Private Sub btnStart_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles btnStart.Click
- Programming Visual Basic 6.0; Francesco Balena; 1999
How to Program the Progress Bar in VB.net
The progress bar control in Visual Basic gives a visual representation of the progress of some function in the program. After adding...
Visual Basic ListView Tutorial
The ListView control in Visual Basic became popular with Windows Explorer. This control works in conjunction with the TreeView control and is...