Interactive demo with VB6 and VB.NET code examples
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.
Try out the choice selection program below. Select your hobbies from the list and see the result update in real-time.
Check all that apply to you
Select at least one hobby to see your choices
Below you'll find the implementation for this choice selection program in both VB6 and VB.NET.
' 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
Modify the VB6 code to:
' 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
Enhance the VB.NET code to:
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 |