Link to home
Start Free TrialLog in
Avatar of santhiran
santhiran

asked on

Please help with the progress bar?

I like to do a progress bar on a form which is updating counts from another form which is doing a process.the "progress form" must also have a cancel button.
Something like when we use a scanner.
Thank you
Avatar of trillo
trillo

Supose
PrgForm = Form with the progress bar
Form1 = Form with iterating process
And now let's say your iterating process looks like:

Private Sub Iterate()
    PrgForm.Show
    PrgForm.ProgressBar1.Min=0
    PrgForm.ProgressBar1.Max=1000
    For i=0 to 1000
          ..... do something.....
        PrgForm.ProgressBar1.Value = i
    Next i
End Sub

This code will work but it has some inconvenients:
1) When you load PrgForm... It can't be modal, because your process wouldn't never update... and if it is not modal, the user could click on Form1, without knowing that a process is running (Form1 would be overlapping PrgForm) and he would think there's something wrong.
2) You should mantain a global variable or flag to know when the user pressed the Cancel button. And you should avoid, when possible, using global variables (it takes more memory).

I would suggest the following:
1) You place your procedure on a module file ( .bas ).
2) When you start the process you invoke PrgForm to load modal, this will avoid user's confusion.
3) On the PrgForm's Activate event you call the process. If you put the code in the Form Load event, you'll never see the progress, that's why you should put it in the Activate event.

It would look something like:
'PrgForm Activate event
Private Sub Form_Activate (...)
    PrgFormCancelled = False   ' Set flag to false
    Me.Refresh     ' Necessary in order to show the controls.
    Call UpdateProc
End Sub

'The Cancel button's Click event
Private Sub Cancel_click (...)
     PrgFormCancelled = True    'Set flag to True
End Sub

You need to declare the PrgFormCancelled flag as public in the module file.
Public PrgFormCancelled as Boolean

Your update procedure (in a module file) would look like:
Public Sub UpdateProc()
    PrgForm.ProgressBar1.Min=0
    PrgForm.ProgressBar1.Max=1000
    For i=0 to 1000
          ..... do something.....
        PrgForm.ProgressBar1.Value = i
        DoEvents
        If PrgFormCancelled = True Then
            Unload PrgForm
        End If
    Next i
End Sub

The DoEvents statement is important, in order to giva a chance to the Cancel button to be pressed.

Trillo
Avatar of santhiran

ASKER

I do not want to use the module file because I want to use the same progress bar form for other forms too. Please help for the process involved in the backgroung form is a bit complex.
Thank you.
I do not want to use the module file because I want to use the same progress bar form for other forms too. Please help for the process involved in the backgroung form is a bit complex.
Thank you.
ASKER CERTIFIED SOLUTION
Avatar of trillo
trillo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial