The code that follows illustrates how the progress form can be employed. First we have to create an instance of the form. This must be placed in the client module’s Declarations section because it will be raising events within this module, much the same way as controls do. Forms and classes that raise events are declared as WithEvents, in the following way:
Private WithEvents frmPiProg As New frmProgress
We must declare the form in this way; otherwise, we wouldn’t have access to the form’s events. By using this code, the form and its events will appear within the Object and Procedure combo boxes in the code window, just as for a control.
Now that the form has been instantiated, we can make use of it during our lengthy process. First we set the form’s initial properties and display it. Then we can continue with our process, updating the form’s ProgressBarValue property as we go, as illustrated here:
' Set up the form's initial properties.
frmPiProg.FormCaption = "File Search"
frmPiProg.ProgressBarMax = 100
frmPiProg.ProgressBarValue = 0
frmPiProg.ProgressCaption = _
"Searching for file. Please wait..."
' Display the progress form.
frmPiProg.Display vbModal
' Find our file.
§
frmPiProg.ProgressBarValue = nPercentComplete
The final piece of code we need to put into our client is the event handler for the QueryAbandon event that the progress form raises when the user clicks the Cancel button. This event gives us the chance to confirm or cancel the abandonment of the current process, generally after seeking confirmation from the user. An example of how this might be done follows:
Private Sub frmPiProg_QueryAbandon(Cancel As Boolean)
If MsgBox("Are you sure you want to cancel?", _
vbQuestion Or vbYesNo, Me.Caption) = vbNo Then
Cancel = True
End If
End Sub
From this example, you can see that in order to use the progress form, the parent code simply has to set the form’s properties, display it, and deal with any events it raises.