Lesson 22: Mastering RadioButton Controls in VB2022

Learn to create exclusive selection interfaces with radio buttons for user preferences and settings

Key Takeaway

Radio buttons enable exclusive selection - when one is chosen, others in the same group are automatically deselected. They're essential for scenarios where users must make a single choice from multiple options.

Welcome to Lesson 22 of our Visual Basic 2022 Tutorial! In this lesson, you'll learn how to effectively implement and utilize radio button controls in your VB2022 applications. Radio buttons (also known as option buttons) are designed for scenarios where users need to select exactly one option from a set of choices.

22.1 Introduction to RadioButton Controls

Radio buttons differ from checkboxes in that they enforce a single selection within a group. When one radio button is selected, all others in the same group are automatically deselected. This makes them perfect for scenarios like:

Common Use Cases

Radio buttons are ideal for:

Application Example
User Profiles Gender selection (Male/Female/Other)
Settings Theme preference (Light/Dark)
Surveys Age range selection
Order Forms Shipping method (Standard/Express)
Quiz Apps Single correct answer selection

Design Tip

Always group related radio buttons visually using GroupBox or Panel containers. This creates logical sections and ensures proper exclusive selection behavior.

Key Properties

Essential radio button properties:

Property Description
Checked Determines if radio button is selected
Text The label displayed next to the radio button
AutoCheck Automatically changes state when clicked
Appearance Determines if shown as button or traditional radio
Tag Store custom data associated with the control
RadioButtonProperties.vb
' Create a radio button dynamically
Dim radNew As New RadioButton()
radNew.Text = "New Option"
radNew.AutoSize = True
radNew.Location = New Point(20, 50)
grpOptions.Controls.Add(radNew)  ' Add to GroupBox

22.2 Practical RadioButton Examples

Let's explore practical implementations of radio button controls in VB2022 applications.

Example 22.1: T-Shirt Selector

A basic interface for selecting T-shirt color using radio buttons.

TShirtSelector.vb
Private Sub BtnConfirm_Click(sender As Object, e As EventArgs) Handles BtnConfirm.Click
    Dim selectedColor As String = ""
    
    ' Check which color is selected
    If radRed.Checked Then
        selectedColor = "Red"
        lblDisplay.ForeColor = Color.Red
    ElseIf radGreen.Checked Then
        selectedColor = "Green"
        lblDisplay.ForeColor = Color.Green
    ElseIf radBlue.Checked Then
        selectedColor = "Blue"
        lblDisplay.ForeColor = Color.Blue
    End If
    
    lblDisplay.Text = "Selected Color: " & selectedColor
End Sub
T-Shirt Color Selector
Figure 22.1: T-Shirt Color Selection Interface

Example 22.2: GroupBox Implementation

Using GroupBox containers to create independent selection groups for size and color.

TShirtSizeColor.vb
Private Sub BtnConfirm_Click(sender As Object, e As EventArgs) Handles BtnConfirm.Click
    Dim size As String = ""
    Dim color As String = ""
    
    ' Process size selection
    If radSmall.Checked Then
        size = "Small"
    ElseIf radMedium.Checked Then
        size = "Medium"
    ElseIf radLarge.Checked Then
        size = "Large"
    End If
    
    ' Process color selection
    If radRed.Checked Then
        color = "Red"
    ElseIf radBlue.Checked Then
        color = "Blue"
    ElseIf radGreen.Checked Then
        color = "Green"
    End If
    
    lblResult.Text = $"Size: {size}, Color: {color}"
End Sub
GroupBox Implementation
Figure 22.2: Size and Color Selection with GroupBox

Example 22.3: Quiz Application

A quiz interface with radio buttons for answer selection and immediate feedback.

QuizApp.vb
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ' Set up the question
    lblQuestion.Text = "What is the capital of France?"
    radOption1.Text = "London"
    radOption2.Text = "Berlin"
    radOption3.Text = "Paris"
    radOption4.Text = "Madrid"
    
    ' Set correct answer (Paris)
    radOption3.Tag = "Correct"
End Sub

Private Sub btnCheck_Click(sender As Object, e As EventArgs) Handles btnCheck.Click
    If radOption1.Checked Then
        ShowFeedback("Incorrect! Try again.", Color.Red)
    ElseIf radOption2.Checked Then
        ShowFeedback("Incorrect! Try again.", Color.Red)
    ElseIf radOption3.Checked Then
        ShowFeedback("Correct! Paris is the capital.", Color.Green)
    ElseIf radOption4.Checked Then
        ShowFeedback("Incorrect! Try again.", Color.Red)
    Else
        ShowFeedback("Please select an answer!", Color.Blue)
    End If
End Sub

Private Sub ShowFeedback(message As String, color As Color)
    lblFeedback.Text = message
    lblFeedback.ForeColor = color
End Sub

Example 22.4: Settings Panel

A settings interface where radio buttons dynamically change form properties.

SettingsPanel.vb
Private Sub btnApply_Click(sender As Object, e As EventArgs) Handles btnApply.Click
    ' Apply theme selection
    If radLightTheme.Checked Then
        ApplyTheme(Color.White, Color.Black)
    ElseIf radDarkTheme.Checked Then
        ApplyTheme(Color.DimGray, Color.White)
    End If
    
    ' Apply font size selection
    If radSmallFont.Checked Then
        ApplyFontSize(10)
    ElseIf radMediumFont.Checked Then
        ApplyFontSize(12)
    ElseIf radLargeFont.Checked Then
        ApplyFontSize(14)
    End If
End Sub

Private Sub ApplyTheme(backColor As Color, foreColor As Color)
    Me.BackColor = backColor
    Me.ForeColor = foreColor
End Sub

Private Sub ApplyFontSize(size As Single)
    ' Apply font size to all controls
    For Each ctrl As Control In Me.Controls
        ctrl.Font = New Font(ctrl.Font.FontFamily, size)
    Next
End Sub

RadioButton Control Summary

Radio buttons are essential for creating exclusive selection interfaces in VB2022 applications:

Feature Description Implementation
Checked Property Determines if radio button is selected If RadioButton1.Checked Then ...
CheckedChanged Event Triggered when selection changes Handles RadioButton1.CheckedChanged
Grouping Create exclusive selection groups Use GroupBox or Panel containers
Dynamic Creation Create radio buttons at runtime Dim rad As New RadioButton()
Default Selection Set a default selected option radDefault.Checked = True
Visual Feedback Provide immediate response to selection Update UI in CheckedChanged event

User Experience

Always provide a default selection and ensure groups are visually distinct for better usability

Accessibility

Use keyboard shortcuts (access keys) and ensure proper tab order for keyboard navigation

Validation

Always check that a selection has been made before processing form submissions

Practical Exercises

Apply your radio button knowledge with these hands-on exercises:

Exercise 1: Survey Form

Create a survey form with three questions (gender, age range, education level). Each question should use radio buttons in its own GroupBox. Add a submit button that displays all selections in a summary label.

Exercise 2: Dynamic Theme Selector

Create a form with radio buttons for theme selection (Light, Dark, Blue). Implement immediate theme change when a radio button is selected, without needing an Apply button.

Exercise 3: Pizza Order System

Design a pizza order form with radio buttons for crust type (Thin, Thick, Stuffed) and size (Small, Medium, Large). Include a calculate button that shows the total price based on selections.

Exercise 4: Dynamic Options Generator

Create a form with a button that generates 5 radio buttons dynamically. When any option is selected, display the selected option in a message box. Include a "Select All" checkbox that doesn't affect radio button behavior.

Next Lesson

Ready to create a web browser? Continue to Lesson 23: Creating Web Browser.

Related Resources

VB6 Tutorial

Mastering VB6 Programming

Explore Tutorials

Visual Basic Examples

Practical VB code samples for real-world applications

View Examples

Excel VBA Tutorial

Learn how to automate Excel by creating VBA macros

Learn More