Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 150
  • Last Modified:

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
0
santhiran
Asked:
santhiran
  • 2
  • 2
1 Solution
 
trilloCommented:
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
0
 
santhiranAuthor Commented:
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.
0
 
santhiranAuthor Commented:
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.
0
 
trilloCommented:
That's not a problem... you could use the same progress form and progress bar for multiple processes.... Just pass the form (or the progressbar, as a parameter of the routine.

Public Sub UpdateProc(PrgForm as Form)
    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

Trillo
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now