Monday, 5 October 2009

Progress Bar VB

Progress Bar VB
Progress bars are a very handy control within the Visual Basic environment.

They can give you a visual indicator of how 'far' through a recordset or dataset your application has reached. When searching through a large number of records it is a good way to let the user know that things are 'still happening' and that the system has not hung.

Here we will give a simple example in VB6

  • Open VB and create a new standard project
  • In the Project menu option, choose 'Components' and scroll down the list. You should come to Microsoft Windows Common Controls 6.0 (Depending on which service pack you have installed)
  • Check it and a tick will appear
  • Click OK and more controls will have appeared in the toolbox
  • VB6 Components Menu - Progress Bar VB Now you can drag a progress bar control onto the form. Place it and a command button on the form. We always rename our controls to something meaningul (EG: cmdSave) but for the purposes of this example we won't bother renaming any controls.

    VB Progress BarNow we will play with a few properties:

    Set the Min property to 1
    Set the Max property to 10
    Set the Scrolling property to ccScrollingSmooth

    Now in the Command1 Code enter the following:

    Dim iIndex As Integer

    'Clear the progress bar - it cannot be less than the min property
    ProgressBar1.Value = 1

    For iIndex = 1 To 9

    'Increment the progress bar
    ProgressBar1.Value = ProgressBar1.Value + 1
    Call Wait(0.1)

    Now paste in the follwing private subroutine:

    Private Sub Wait(ByVal iHowManySecs As Integer)
    Dim dStart As Date

    On Error GoTo WaitErr

    dStart = Now

    Do Until DateDiff("s", dStart, Now) > iHowManySecs
    'Waiting until number of seconds has passed

    Exit Sub

    Resume WaitExit

    End Sub

    Run the application and click the command button. A nice simple smooth scrolling progress bar.

    Of course progress bars are most often used when looping through a recordset which we will cover in the future.

    Progress Bar VB

    1 comment:

    1. Unless your sticking in a Doevents (which lives on in 3.5 framework) why not use Sleep() in Kernel32