What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleMake a wizard that uses buttons for navigation in VB .NET
DescriptionThis example shows how to make a wizard that uses buttons for navigation in VB .NET. The user clicks buttons to move between pages. The wizard only enables a button if all previous data is valid. It only enables the OK button when all of the data is filled in.
Keywordswizard, button wizard
CategoriesSoftware Engineering
The program stores each page of fields in a separate GroupBox. For convenience, these are spread around the form and the form is extra large so you can see them all. When it loads, the form moves all of the GroupBoxes to the same position and resizes itself to fit nicely.

The Load event handler also adds the event handler SomethingChanged to all of the fields. Whenever the user modified any of them, SomethingChanged enables and disables the appropriate buttons.

' Arrange the controls and form.
Private Sub wizEmployee_Load(...) Handles MyBase.Load
    Me.ClientSize = New Size(btnOK.Left + btnOK.Width + 8, _
        btnName.Top + btnName.Height + 8)

    grpAddress.Bounds = grpName.Bounds
    grpPrivilege.Bounds = grpName.Bounds

    grpName.Visible = True
    grpAddress.Visible = False
    grpPrivilege.Visible = False
    m_VisibleGroupBox = grpName

    ' Fire the SomethingChanged event handler if anything
    ' changes.
    AddHandler txtFirstName.TextChanged, AddressOf _
    AddHandler txtLastName.TextChanged, AddressOf _
    AddHandler txtPhone.TextChanged, AddressOf _
    AddHandler txtStreet.TextChanged, AddressOf _
    AddHandler txtCity.TextChanged, AddressOf _
    AddHandler txtState.TextChanged, AddressOf _
    AddHandler txtZip.TextChanged, AddressOf _
    AddHandler radClerk.Click, AddressOf SomethingChanged
    AddHandler radDispatcher.Click, AddressOf _
    AddHandler radSupervisor.Click, AddressOf _
    AddHandler radSystemAdministrator.Click, AddressOf _

End Sub
Subroutine SomethingChanged enables the buttons that are appropriate for the data that has been entered. This example uses three pages of data: Name, Address, and Privilege. The Name button is always enabled. The Address button is enabled if all of the TextBoxes on the Name page are filled in. The Privilege button is enabled if all of the TextBoxes on the Name and Address pages are filled in. The OK button is enabled if every field is filled in.

After enabling and disabling buttons, SomethingChanged sets the form's Accept button appropriately. For example, if the user has filled in the Name fields, then it sets the form's AcceptButton property to the Address button so if the user presses Enter, the wizard displays the Address page.

' Enable the appropriate buttons.
Private Sub SomethingChanged(...) Handles _
    btnAddress.Enabled = _
        txtFirstName.Text.Length > 0 AndAlso _
        txtLastName.Text.Length > 0 AndAlso _
        txtPhone.Text.Length > 0
    btnPrivilege.Enabled = _
        btnAddress.Enabled AndAlso _
        txtStreet.Text.Length > 0 AndAlso _
        txtCity.Text.Length > 0 AndAlso _
        txtState.Text.Length > 0 AndAlso _
        txtZip.Text.Length > 0
    btnOK.Enabled = _
        btnPrivilege.Enabled AndAlso _
        Not (m_Privilege Is Nothing)

    ' Set the Accept button.
    If Not btnAddress.Enabled Then
        Me.AcceptButton = btnName
    ElseIf Not btnPrivilege.Enabled Then
        Me.AcceptButton = btnAddress
    ElseIf Not btnOK.Enabled Then
        Me.AcceptButton = btnPrivilege
        Me.AcceptButton = btnOK
    End If
End Sub
When the user clicks a page button, the code hides the currently visible GropuBox and displays the new one. For example, the following code shows how the program displays the Name page.
Private Sub btnName_Click(...) Handles btnName.Click
    If m_VisibleGroupBox Is grpName Then Exit Sub

    m_VisibleGroupBox.Visible = False
    m_VisibleGroupBox = grpName
    m_VisibleGroupBox.Visible = True

End Sub
When the user clicks the OK button, the code simply sets the form's DialogResult property to DialogResult.OK. That automatically closes the wizard and sets the return value for its ShowDialog method.

Note that the OK button is only enabled if all of the fields have been filled in so this event handler doesn't need to perform any validation.

' Close the wizard.
Private Sub btnOK_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnOK.Click
    Me.DialogResult = DialogResult.OK
End Sub
Function EmployeeXml returns the result composed by the wizard. This output is the wizard's goal.
' Return the Employee's XML text (the thing this wizard
' builds).
Public Function EmployeeXml() As String
    Dim txt As String
    txt = "<Employee>" & vbCrLf
    txt &= "    <FirstName>" & Me.txtFirstName.Text & _
        "</FirstName>" & vbCrLf
    txt &= "    <LastName>" & Me.txtLastName.Text & _
        "</LastName>" & vbCrLf
    txt &= "    <Phone>" & Me.txtPhone.Text & "</Phone>" & _
    txt &= "    <Street>" & Me.txtStreet.Text & "</Street>" _
        & vbCrLf
    txt &= "    <City>" & Me.txtCity.Text & "</City>" & _
    txt &= "    <State>" & Me.txtState.Text & "</State>" & _
    txt &= "    <Zip>" & Me.txtZip.Text & "</Zip>" & vbCrLf
    txt &= "    <Privilege>" & m_Privilege & "</Privilege>" _
        & vbCrLf
    txt &= "</Employee>" & vbCrLf

    Return txt
End Function
The main program calls the wizard's ShowDialog method. If the user fills in all the forms and clicks OK, then ShowDialog returns DialogResult.OK, the program calls the wizard's EmployeeXml function and displays the results.
Private Sub mnuDataMakeEmployee_Click(...) Handles _
    Dim wiz As New wizEmployee
    If wiz.ShowDialog() = DialogResult.OK Then
        txtResults.Text = wiz.EmployeeXml()
        txtResults.Select(0, 0)
    End If
End Sub
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.