Lesson 22: Working with Radio Button Controls

Master exclusive selections with radio button controls

Key Takeaway

Radio button controls allow users to select only one option from a set of choices. They are essential for mutually exclusive selections like gender, age groups, or preference settings.

In the previous lesson, we explored checkbox controls for multiple selections. Now we'll learn how to implement radio button controls to create exclusive choice interfaces. Radio buttons allow users to select only one item from a group of options, making them perfect for forms with mutually exclusive choices.

22.1 Understanding Radio Button Controls

Radio button controls provide users with exclusive choices where only one selection is allowed per group. They're different from checkboxes which allow multiple selections.

Exclusive Selection

Users can select only one option per group

Grouping

Radio buttons are grouped using GroupBox containers

Binary State

Each radio button has a selected or unselected state

Practical Applications

Ideal for surveys, settings, and preference selections

22.1(a) Simple Radio Button Selection

In this example, users can select one T-shirt color from three options. The interface includes three radio buttons and a confirmation button.

Form1.vb
Public Class Form1

Private Sub BtnConfirm_Click(sender As Object, e As EventArgs) Handles BtnConfirm.Click
    ' Declare color variable
    Dim Tcolor As String
    
    ' Check each radio button selection
    If RadioRed.Checked Then
        Tcolor = "Red Color"
        LblDisplay.ForeColor = Color.Red
    ElseIf RadioGreen.Checked Then
        Tcolor = "Green Color"
        LblDisplay.ForeColor = Color.Green
    Else
        Tcolor = "Yellow Color"
        LblDisplay.ForeColor = Color.Yellow
    End If
    
    ' Display the selected color
    LblDisplay.Text = Tcolor
End Sub
End Class
Radio Button Color Selection

Figure 22.1: T-shirt color selection with radio buttons

22.1(b) Grouped Radio Buttons

This example demonstrates how to use GroupBox containers to create independent radio button groups for size and color selections.

Form1.vb
Public Class Form1

Private Sub BtnConfirm_Click(sender As Object, e As EventArgs) Handles BtnConfirm.Click
    ' Declare variables for size and color
    Dim TSize, TColor As String
    
    ' Check size selections
    If RadioXL.Checked Then
        TSize = "XL"
    ElseIf RadioL.Checked Then
        TSize = "L"
    ElseIf RadioM.Checked Then
        TSize = "M"
    Else
        TSize = "S"
    End If
    
    ' Check color selections
    If RadioRed.Checked Then
        TColor = "Red"
    ElseIf RadioBlue.Checked Then
        TColor = "Blue"
    Else
        TColor = "Beige"
    End If
    
    ' Display selections
    LblSize.Text = TSize
    LblColor.Text = TColor
End Sub
End Class
Grouped Radio Buttons

Figure 22.2: Grouped radio buttons for size and color

Grouping Radio Buttons

Radio buttons are mutually exclusive within their container group. To have multiple sets of radio buttons on the same form:

  • Use GroupBox controls to create separate containers
  • Place related radio buttons inside each GroupBox
  • Radio buttons in different GroupBoxes are independent
  • Each GroupBox can have its own selected radio button

In Visual Studio, drag a GroupBox from the Containers category in the Toolbox, then add radio buttons inside it.

Lesson Summary

In this lesson, you've learned how to effectively implement radio button controls in VB2019:

Exclusive Selection

Created interfaces with mutually exclusive choices

Grouping Techniques

Used GroupBox containers for multiple radio button groups

Selection Handling

Implemented code to process user selections

Practical Applications

Applied radio buttons to solve real-world interface challenges

Radio button controls are essential for creating interfaces where users need to make exclusive choices. In the next lesson, we'll explore how to create a web browser application.

Next Lesson

Ready to build a web browser? Continue to Lesson 23: Creating a 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

VB2019 Paperback

Comprehensive guide to Visual Basic 2019

Get on Amazon