Updating a Gauge and Detecting a User Interrupt (Visual Basic)

Take the following steps to update a gauge during processing and detect a user interrupt:

1. Start with the project that you created in Loading and Displaying an Image.

2. image\btncmd.gif Select the CommandButton control; then add the control to your main form. Put the control at the top of the form to keep it away from the image.

3. In the Properties box, change the CommandButton control's Caption property to Do Median.

4. image\btncmd.gif Select the CommandButton control; then add another control to your main form. Put the control at the top of the form to keep it away from the image.

5. In the Properties box, change the CommandButton control's Caption property to Quit.

6. Build a gauge consisting of a horizontal line image\btnline.gif (Line1) inside a rectangle image\btnshape.gif (Shape1). Put the gauge at the top of the form next to the Quit button.

7. Add the following code to the general declarations:

Dim fEscape As Integer 'Use to indicate the user interrupt
Dim fInProc As Integer 'Use to avoid closing the form during processing

8. Add the following code to the first CommandButton control's Click procedure:

Private Sub Command1_Click ()
    'Enable the ProgressStatus event
    Lead1.EnableProgressEvent = True

    'Initialize the indicators
    fEscape = False 'The user does not want to quit
    fInProc = True 'Processing is taking place

    'Perform a relatively slow median filter
    Lead1.Median 4 

    'Clean up
    fInProc = False 'Processing is no longer taking place
    Lead1.ForceRepaint
End Sub

9. Add the following code to the Lead1 control's ProgressStatus procedure:

Private Sub Lead1_ProgressStatus(ByVal iPercent As Integer)
    DoEvents 'Detect Windows events
    If Not fEscape Then ' Look for the Click on the Quit button
      LineLength = Form1.Shape1.Width * iPercent / 100 'Udpate the gauge
      Me.Line1.X1 = Me.Shape1.Left
      Me.Line1.X2 = Me.Line1.X1 + LineLength
    Else
      Lead1.EnableProgressEvent = False 'Cancel the task
    End If
End Sub

10. Add the following code to the second CommandButton control's Click procedure:

Private Sub Command2_Click ()
  fEscape = True 'The user wants to quit
  Me.Line1.X2 = Me.Line1.X1 'Set the gauge back to the beginning
End Sub

11. Add the following code to the main form's QueryUnload procedure:

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  If fInProc Then 'If processing is taking place
    Cancel = 1 'Do not let the user close the form
  End If
End Sub

  1. 12. Run your program to test it.