Results 1 to 1 of 1

Thread: Classic VB - How can I use a progress bar?

  1. #1

    Thread Starter
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Classic VB - How can I use a progress bar?

    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:
    1. Dim intCount as Integer
    2.   ProgressBar1.Min = 0    'Initialise progress bar
    3.   ProgressBar1.Max = 10
    4.   ProgressBar1.Value = 0
    5.   ProgressBar1.Refresh
    6.  
    7.   For intCount = 1 To 10
    8.       'your code here:  (this just pauses for a second)
    9. Dim datPauseUntil As Date
    10.     datPauseUntil = DateAdd("s", 1, Now)
    11.     Do
    12.       DoEvents
    13.     Loop Until Now > datPauseUntil
    14.  
    15.  
    16.       'set the new progress value (use just one of these lines):
    17.     ProgressBar1.Value = intCount
    18.     'ProgressBar1.Value = ProgressBar1.Value + 1
    19.  
    20.       'make sure it updates on-screen:
    21.     ProgressBar1.Refresh
    22.   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:
    1. ProgressBar1.Min = 0    'Initialise progress bar
    2.   ProgressBar1.Max = 2  'number of "sections"
    3.   ProgressBar1.Value = 0
    4.   ProgressBar1.Refresh
    5.  
    6.   '(code section 1)
    7.  
    8.     'set the new progress value, and update it on-screen
    9.   ProgressBar1.Value = 1
    10.   ProgressBar1.Refresh
    11.  
    12.   '(code section 2)
    13.  
    14.     'set the new progress value, and update it on-screen
    15.   ProgressBar1.Value = 2
    16.   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:
    1. Me.MousePointer = vbHourglass  'set mouse icon to "wait"
    2.  
    3.     'your loop etc here
    4.  
    5.   Me.MousePointer = vbDefault    'return mouse icon to normal
    Last edited by si_the_geek; Nov 25th, 2006 at 01:13 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width