Programming the wizard buttons

Each of the four wizard buttons requires a procedure to handle its Click event. The event handler for CancelButton follows. This procedure uses a MsgBox function (see Figure 15-5) to verify that the user really wants to exit. If the user clicks the Yes button, the UserForm is unloaded with no action taken. This type of verification, of course, is optional.

Private Sub CancelButton_Click() Msg = "Cancel the wizard?"

Ans = MsgBox(Msg, vbQuestion + vbYesNo, APPNAME) If Ans = vbYes Then Unload Me End Sub

The event handler procedures for the Back and Next buttons follow:

Private Sub BackButton_Click()

MultiPagel.Value = MultiPagel.Value - 1 UpdateControls End Sub

Private Sub NextButton_Click()

MultiPagel.Value = MultiPagel.Value + 1 UpdateControls End Sub

Figure 15-5: Clicking the Cancel button displays a message box.

Figure 15-5: Clicking the Cancel button displays a message box.

These two procedures are very simple. They change the Value property of the MultiPage control and then call another procedure named UpdateControls (which follows).

The UpdateControls procedure in Listing 15-1 is responsible for enabling and disabling the BackButton and NextButton controls.

Listing 15-1: Procedures That Enable the Key Controls in the Wizard

Sub UpdateControls()

Select Case MultiPagel.Value Case 0

BackButton.Enabled = False NextButton.Enabled = True Case MultiPagel.Pages.Count - l BackButton.Enabled = True NextButton.Enabled = False Case Else

BackButton.Enabled = True NextButton.Enabled = True End Select

' Update the caption

Me.Caption = APPNAME & " Step " _ & MultiPagel.Value + l & " of " _ & MultiPagel.Pages.Count


Listing 15-1 (Continued)

The Name field is required If tbName.Text = "" Then

FinishButton.Enabled = False


FinishButton.Enabled = True End If End Sub

The procedure changes the UserForm's caption to display the current step and the total number of steps (APPNAME is a public constant, defined in Module1). It then examines the name field on the first page (a TextBox named tbName). This is a required field, so the Finish button can't be clicked if it's empty. If the TextBox is empty, the FinishButton is disabled; otherwise, it's enabled.

0 0

Post a comment