VB Tutor VB.NET 2022 Tutorial VB2019 Tutorial VB6 Tutorial VB Sample Code About Us
Visual Basic Sample Code

Choice Selection Program

Interactive demo with VB6 and VB.NET code examples


Introduction to Choice Selection

Choice selection is a fundamental programming concept that allows users to make selections from a list of options. In Visual Basic, this is commonly implemented using checkboxes which have two states: checked or unchecked.

In this demo, we'll show you how to create a choice selection program in both Visual Basic 6 and VB.NET. The program presents users with three hobbies to choose from, and then displays a message showing their selected choices.

Tip: The checkbox control's value is represented by Checkbox.Value = vbChecked or Checkbox.Value = vbUnchecked in VB6, and CheckBox.Checked = True in VB.NET.

Interactive Demo

Try out the choice selection program below. Select your hobbies from the list and see the result update in real-time.

Select Your Hobbies

Check all that apply to you

Select at least one hobby to see your choices

Visual Basic Code Examples

Below you'll find the implementation for this choice selection program in both VB6 and VB.NET.

Visual Basic 6 Implementation

' Choice Selection Program in VB6
Private Sub Command1_Click()
    If Check1.Value = vbChecked And Check2.Value = vbChecked And Check3.Value = vbChecked Then
        MsgBox "You like Reading, Computer and Sports"
    ElseIf Check1.Value = vbChecked And Check2.Value = vbChecked And Check3.Value = vbUnchecked Then
        MsgBox "You like Reading and Computer"
    ElseIf Check1.Value = vbChecked And Check2.Value = vbUnchecked And Check3.Value = vbChecked Then
        MsgBox "You like Reading and Sports"
    ElseIf Check1.Value = vbUnchecked And Check2.Value = vbChecked And Check3.Value = vbChecked Then
        MsgBox "You like Computer and Sports"
    ElseIf Check1.Value = vbChecked And Check2.Value = vbUnchecked And Check3.Value = vbUnchecked Then
        MsgBox "You like Reading only"
    ElseIf Check1.Value = vbUnchecked And Check2.Value = vbChecked And Check3.Value = vbUnchecked Then
        MsgBox "You like computer only"
    ElseIf Check1.Value = vbUnchecked And Check2.Value = vbUnchecked And Check3.Value = vbChecked Then
        MsgBox "You like Sports only"
    Else
        MsgBox "You have no hobby"
    End If
End Sub

VB6 Exercise

Modify the VB6 code to:

  • Add a fourth hobby option (e.g. Music)
  • Create a more efficient approach using arrays or collections
  • Display the results in a label instead of a message box

VB.NET Implementation

' Choice Selection Program in VB.NET
Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
    Dim selectedHobbies As New List(Of String)
    
    ' Check which checkboxes are selected
    If chkReading.Checked Then selectedHobbies.Add("Reading")
    If chkComputer.Checked Then selectedHobbies.Add("Computer")
    If chkSports.Checked Then selectedHobbies.Add("Sports")
    
    ' Determine the message based on selections
    Dim message As String = ""
    
    Select Case selectedHobbies.Count
        Case 0
            message = "You have no hobby"
        Case 1
            message = "You like " & selectedHobbies(0) & " only"
        Case 2
            message = "You like " & selectedHobbies(0) & " and " & selectedHobbies(1)
        Case 3
            message = "You like Reading, Computer and Sports"
    End Select
    
    ' Display the result
    lblResult.Text = message
    ' Or using a message box: MessageBox.Show(message)
End Sub

VB.NET Exercise

Enhance the VB.NET code to:

  • Add a "Select All" checkbox that selects/deselects all options
  • Create a method to dynamically generate hobby options from a list
  • Add a counter that shows how many hobbies are selected

VB6 vs. VB.NET Comparison

Here's a comparison of how choice selection is implemented in VB6 vs. VB.NET:

Feature VB6 VB.NET
Checkbox Property Value (vbChecked/vbUnchecked) Checked (True/False)
Event Handling Click event on command button Click event handler with sender parameter
Message Display MsgBox function MessageBox.Show or label control
Data Structures Limited, requires complex conditionals Collections, Lists, and other advanced data structures
Code Efficiency Requires multiple ElseIf statements More efficient with collections and loops
Scalability Hard to add new options Easier to add new options dynamically

Best Practice: The VB.NET implementation is more scalable and maintainable. If you need to add more hobbies, you can simply add them to the collection without modifying the core logic.