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-10) 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() Dim Msg As String Dim Ans As Integer Msg = "Cancel the wizard?"

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

Wizard Demo

Cancel the wizard?

Figure 15-10: Clicking the Cancel button displays a confirmation message box. The event handler procedures for the Back and Next buttons follow:

Private Sub BackButton_Click()

MultiPage1.Value = MultiPage1.Value UpdateControls End Sub

Private Sub NextButton_Click()

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

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 is responsible for enabling and disabling the BackButton and NextButton controls.

Sub UpdateControls ( )

Select Case MultiPage1.Value Case 0

BackButton.Enabled = False NextButton.Enabled = True Case MultiPage1.Pages.Count - 1 BackButton.Enabled = True NextButton.Enabled = False Case Else

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

Update the caption Me.Caption = APPNAME & " Step " & MultiPage1.Value + 1 & " of & MultiPage1.Pages.Count

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

FinishButton.Enabled = False

Else

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. The procedure then examines the name field on the first page (a TextBox named tbName). This is a required field, so the user can't click the Finish button if it's empty. If the TextBox is empty, the FinishButton is disabled; otherwise, it's enabled.

0 0

Post a comment