Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleMake a tabbed wizard in VB .NET
DescriptionThis example shows how to make a tabbed wizard in VB .NET. The user clicks the tabs to move between pages. When the user clicks OK, the wizard validates its data and displays an error message if necessary.
Keywordswizard, tabbed wizard
CategoriesSoftware Engineering
 
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 _
    tabPages.SelectedIndexChanged
    ' Find the control with smallest TabIndex 
    ' in this tab page.
    Dim best_index As Integer = 100000
    Dim best_control As Control
    For Each ctl As Control In tabPages.SelectedTab.Controls
        If ctl.TabStop AndAlso ctl.TabIndex < best_index _
            Then
            best_control = ctl
            best_index = ctl.TabIndex
        End If
    Next ctl

    ' Set focus to the control.
    If Not (best_control Is 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 = 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, _
            TabPage)

        ' Set focus to the TextBox.
        text_box.Focus()

        ' Display an error message.
        Dim field_name As String = _
            AddSpacesBeforeCaps(text_box.Name.Substring(3))
        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>" & _
        vbCrLf
    txt &= "    <Street>" & Me.txtStreet.Text & "</Street>" _
        & vbCrLf
    txt &= "    <City>" & Me.txtCity.Text & "</City>" & _
        vbCrLf
    txt &= "    <State>" & Me.txtState.Text & "</State>" & _
        vbCrLf
    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 _
    mnuDataMakeEmployee.Click
    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.
  Updated