What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleMake a tabbed wizard in Visual Basic 2005
DescriptionThis example shows how to make a tabbed wizard in Visual Basic 2005.
Keywordswizard, tabbed wizard, VB2005
CategoriesSoftware Engineering, VB.NET
This is a Visual Basic 2005 version of the example Make a tabbed wizard in VB .NET. That version required some changes when converting to Visual Basic 2005.

The wizard saves its pages of fields on different TabControl tabs. When the user clicks on a tab, the SelectedIndexChanged event handler sets focus to the control on that tab with the smallest TabIndex value.

' The user moved to a new tab. Set the focus to the
' tab's first control.
Private Sub tabPages_SelectedIndexChanged(ByVal sender As _
    Object, ByVal e As System.EventArgs) Handles _
    ' Find the control with smallest TabIndex 
    ' in this tab page.
    Dim best_index As Integer = 100000
    Dim best_control As Control = Nothing
    For Each ctl As Control In tabPages.SelectedTab.Controls
        If ctl.TabStop AndAlso ctl.TabIndex < best_index _
            best_control = ctl
            best_index = ctl.TabIndex
        End If
    Next ctl

    ' Set focus to the control.
    If best_control IsNot Nothing Then best_control.Focus()
End Sub
When the user clicks OK, the program checks that each of its TextBoxes has a non-blank value. If all fields are valid, the program sets the form's DialogResult property to DialogResult.OK. That closes the form and sets its return value.

If a TextBox is empty, the TextBoxIsEmpty function displays an error message, sets focus to the TextBox, and returns True. Function AddSpacesBeforeCaps helps TextBoxIsEmpty format the TextBox's name for the error message.

' Validate the data and close the wizard.
Private Sub btnOK_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnOK.Click
    ' Validate the data.
    If TextBoxIsEmpty(txtFirstName) Then Exit Sub
    If TextBoxIsEmpty(txtLastName) Then Exit Sub
    If TextBoxIsEmpty(txtPhone) Then Exit Sub

    If TextBoxIsEmpty(txtStreet) Then Exit Sub
    If TextBoxIsEmpty(txtCity) Then Exit Sub
    If TextBoxIsEmpty(txtState) Then Exit Sub
    If TextBoxIsEmpty(txtZip) Then Exit Sub

    If m_Privilege Is Nothing Then
        tabPages.SelectedTab = pagPrivilege
        MessageBox.Show("Please select a privilege level.", _
            "Enter Value", _
            MessageBoxButtons.OK, MessageBoxIcon.Error)
        Exit Sub
    End If

    Me.DialogResult = Windows.Forms.DialogResult.OK
End Sub

' If this TextBox holds a blank string, set focus to it,
' display its tab, display an error message, and return
' True.
Private Function TextBoxIsEmpty(ByVal text_box As TextBox) _
    As Boolean
    If text_box.Text.Length = 0 Then
        ' Display the tab containing the TextBox.
        tabPages.SelectedTab = DirectCast(text_box.Parent, _

        ' Set focus to the TextBox.

        ' Display an error message.
        Dim field_name As String = _
        MessageBox.Show("The " & field_name & _
            " must not be blank.", "Enter Value", _
            MessageBoxButtons.OK, MessageBoxIcon.Error)
        Return True
    End If

    Return False
End Function

' Insert spaces before the string's capital letters.
Private Function AddSpacesBeforeCaps(ByVal txt As String) _
    As String
    Dim txt_chars() As Char = txt.ToCharArray
    Dim result As String = txt_chars(0)

    For i As Integer = 1 To txt.Length - 1
        If (txt_chars(i) >= "A"c) And (txt_chars(i) <= _
            "Z"c) Then
            result &= " "
        End If
        result &= txt_chars(i)
    Next i

    Return result
End Function
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
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.