# Visual Basic > Visual Basic FAQs >  Classic VB - How can I use a progress bar?

## si_the_geek

When you look in your Toolbox (the window that lets you select a Textbox/PictureBox/... to put on your form) you will probably not see a progress bar, but there is one hidden away that you can use!


*Adding the progress bar to your form*
To get the progress bar into the toolbox you will need to add an extra component, which you can do by going to the "Project" menu, then "Components".  The components window will then open, and you need to tick the box "Microsoft Windows Common Controls 6.0 _(SP6)_" (the part shown in italics may be missing, or have a lower number), then click the "OK" button.

You can now place the control on to your form, and size it as you wish.


*Showing progress*
In order to show progress, there are three properties of the control that you need to use, which are:  .Min, .Max, and .Value

.Min is the lowest value (which will usually be set to 0)
.Max is the highest value (which will be the 'To' value if you are using a For loop, as in the example below)
.Value is the current value - halfway between .Min and .Max will be shown as 50% progress.

Here is an example of the code you could use (assuming that your code runs in a loop):

VB Code:
Dim intCount as Integer
  ProgressBar1.Min = 0    'Initialise progress bar
  ProgressBar1.Max = 10 
  ProgressBar1.Value = 0
  ProgressBar1.Refresh
   For intCount = 1 To 10
      'your code here:  (this just pauses for a second)
Dim datPauseUntil As Date
    datPauseUntil = DateAdd("s", 1, Now)
    Do
      DoEvents
    Loop Until Now > datPauseUntil
        'set the new progress value (use just one of these lines):
    ProgressBar1.Value = intCount
    'ProgressBar1.Value = ProgressBar1.Value + 1 
       'make sure it updates on-screen:
    ProgressBar1.Refresh
  Next intCount
If your code is not in a loop then you need to use a slightly different method, updating the .Value in appropriate places.  If you have several "sections" of code, you could use this kind of arrangement (this example is for a total of 2 sections):

VB Code:
ProgressBar1.Min = 0    'Initialise progress bar
  ProgressBar1.Max = 2  'number of "sections"
  ProgressBar1.Value = 0
  ProgressBar1.Refresh
   '(code section 1)
     'set the new progress value, and update it on-screen
  ProgressBar1.Value = 1
  ProgressBar1.Refresh
   '(code section 2)
     'set the new progress value, and update it on-screen
  ProgressBar1.Value = 2
  ProgressBar1.Refresh

*If I don't know my '.Max', what can I do?*
Well I'm afraid the progress bar is not appropriate in this case... if _you_ cannot tell how much work (or time) is left to go, then the progress bar cannot know either!

If you pick a 'random' value for the .Max you will probably find that it is too high (_so even when your code is finished, the bar is not close to being full_), or too low (_so it gets full long before your code is finished_).

The best thing you can do is let the user know that something is happening, and there are a few common methods that can be used, such as:
Have a label (or some other form of text) which tells the user that work is in progress.
Have a label as above, but alter it as the program progresses; for example by adding a changing number of dots after the text:
Loading.Loading..Loading...Loading(repeat until your code has finished)
Place one PictureBox on your form (shaped like a progress bar), and place another (much smaller) one inside it, with a different background colour.

As your code progresses, increase the .Left property of the 'inside' PictureBox (reset it to 0 when the .Left is larger than the .Width of the 'outer' PictureBox)
Show an animation (possibly an AVI), as can often be seen in the top of web browsers when a page is loading.

Dont forget that whatever method you use, you can also set the mouse icon to an hourglass, eg:

VB Code:
Me.MousePointer = vbHourglass  'set mouse icon to "wait"
     'your loop etc here
   Me.MousePointer = vbDefault    'return mouse icon to normal

----------

