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.
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
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.
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
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
Visual Basic 2019 Made Easy
Unlock the power of Visual Basic 2019 with this comprehensive, easy-to-follow handbook written by Dr. Liew, renowned educator and founder of the popular programming tutorial website VBtutor.net. Whether you're new to programming or brushing up your skills, this book is your perfect companion to learn Visual Basic 2019 from the ground up.
What You'll Learn:
- Understand Core Programming Concepts: Grasp the foundational principles of Visual Basic 2019, including variables, data types, conditional logic, loops, and event-driven programming.
- Develop Real Windows Desktop Applications: Build fully functional and interactive Windows apps using Visual Studio 2019—guided through step-by-step tutorials.
- Apply Dozens of Ready-to-Use Examples: Explore a rich collection of practical sample programs, from basic calculators to image viewers and database applications.
- Adapt and Reuse Code for Your Own Projects: Customize professionally written code snippets to speed up your development process and bring your ideas to life.
- Package and Deploy Like a Pro: Learn how to compile, test, and distribute your Visual Basic applications seamlessly with built-in deployment tools.
Visual Basic Programming With Code Examples
Visual Basic Programming with Code Examples offers a unique dual-format approach, showcasing sample codes in both Visual Basic 6 (VB6) and VB.NET. This side-by-side presentation helps you understand the evolution of Visual Basic and empowers you to work confidently across both environments.
What You'll Learn:
- Core Concepts Made Easy: Explore data types, control structures, file handling, procedures, user interface design, and more.
- Hands-On Application Building: Design real-world applications, including financial calculators, educational tools, games, multimedia apps, and database systems.
- 48 Practical Code Examples: Study and customize fully explained programs that illustrate key programming techniques.
- Dual-Code Format: Learn to translate and adapt code between VB6 and VB.NET seamlessly.