View contents of this chapter

The Cancel Button

You code must determine if the user selected a color and clicked OK, or if the user clicked the cancel button. To test whether users have clicked clicked Cancel, you need to learn a new VB command-the  On Error GoTo  statement. This statement enables an error-handling routine and specifies the location of the routine within a procedure. Therefore, the statement

On Error GoTo dlgErrHandler

tells VB to jump to the code labeled dlgErrHandler if an error occurs during any line that follows the On Error GoTo statement. Consider the following code segment:

1: Private Sub Command1_Click ()

2:     ' forces an error if user clicks cancel
3:     dlgColor.CancelError = True

4:     On Error GoTo dlgErrHandler

5:     ' Set the Flags property.
6:     dlgColor.Flags = cdlCCFullOpen

7:     ' Display the Color dialog box.
8:     dlgColor.ShowColor

9:     ' Set the form's background color to the selected color
10:     frmDemo.BackColor = dlgColor.Color
11:     Exit Sub

12:     dlgErrHandler: ' User pressed Cancel button.
13:     Exit Sub
14: End Sub

If the user selects a color and clicks OK, line 10 will be executed. If the user clicks the Cancel button, statements after the error-handling code label are executed if an error occurs in the procedure and an Exit statement can terminate the procedure early. VB triggers an error condition if user clicks the Cancel button.

© Universal Teacher Publications        INDEX Previous Screen Next Screen