Lesson 21: Working with Checkbox Controls
Master user interface design with versatile checkbox controls
Key Takeaway
Checkbox controls allow users to select multiple options simultaneously. They are essential for creating interactive forms, configuration settings, and shopping interfaces.
In the previous lesson, we explored the Format function for professional data presentation. Now we'll learn how to implement checkbox controls to create interactive user interfaces. Checkboxes allow users to select one or more items from a set of options, making them perfect for forms, settings panels, and shopping carts.
21.1 Understanding Checkbox Controls
Checkbox controls provide users with binary choices where multiple selections are allowed. They're different from radio buttons which only allow a single selection from a group.
Multiple Selection
Users can select multiple options simultaneously
Binary State
Each checkbox has a checked or unchecked state
Practical Applications
Ideal for shopping carts, preferences, and forms
Event Handling
Respond to user selections with CheckedChanged events
21.1(a) Shopping Cart Example
In this example, we create a shopping cart interface where users can select items using checkboxes. The total price is calculated based on the selected items.
Public Class Form1 Private Sub BtnCal_Click(sender As Object, e As EventArgs) Handles BtnCal.Click ' Declare item prices as constants Const LX As Integer = 100 Const BN As Integer = 500 Const SD As Integer = 200 Const HD As Integer = 80 Const HM As Integer = 300 Const AM As Integer = 150 Dim sum As Integer = 0 ' Check each checkbox and add to total if selected If CheckBox1.Checked = True Then sum += LX End If If CheckBox2.Checked = True Then sum += BN End If If CheckBox3.Checked = True Then sum += SD End If If CheckBox4.Checked = True Then sum += HD End If If CheckBox5.Checked = True Then sum += HM End If If CheckBox6.Checked = True Then sum += AM End If ' Display formatted total LblTotal.Text = sum.ToString("c") End Sub Private Sub BtnReset_Click(sender As Object, e As EventArgs) Handles BtnReset.Click ' Reset all checkboxes CheckBox1.Checked = False CheckBox2.Checked = False CheckBox3.Checked = False CheckBox4.Checked = False CheckBox5.Checked = False CheckBox6.Checked = False LblTotal.Text = "" End Sub End Class
Figure 21.1: Shopping cart interface with checkbox selections
21.1(b) Simple Checkbox Calculation
This example demonstrates a simpler implementation of checkbox calculations for different size options.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ' Declare size prices Const large As Integer = 10 Const medium As Integer = 8 Const small As Integer = 5 Dim sum As Integer = 0 ' Check each size option If CheckBox1.Checked = True Then sum += large End If If CheckBox2.Checked = True Then sum += medium End If If CheckBox3.Checked = True Then sum += small End If ' Display formatted total Label5.Text = sum.ToString("c") End Sub
21.2 Text Formatting with Checkboxes
Checkboxes are perfect for text formatting controls where users can apply multiple styles simultaneously.
This example demonstrates how to use checkboxes to apply multiple font styles to a label:
Public Class Form1 Private Sub ChkBold_CheckedChanged(sender As Object, e As EventArgs) Handles ChkBold.CheckedChanged If ChkBold.Checked Then ' Apply bold style LblDisplay.Font = New Font(LblDisplay.Font, LblDisplay.Font.Style Or FontStyle.Bold) Else ' Remove bold style LblDisplay.Font = New Font(LblDisplay.Font, LblDisplay.Font.Style And Not FontStyle.Bold) End If End Sub Private Sub ChkItalic_CheckedChanged(sender As Object, e As EventArgs) Handles ChkItalic.CheckedChanged If ChkItalic.Checked Then ' Apply italic style LblDisplay.Font = New Font(LblDisplay.Font, LblDisplay.Font.Style Or FontStyle.Italic) Else ' Remove italic style LblDisplay.Font = New Font(LblDisplay.Font, LblDisplay.Font.Style And Not FontStyle.Italic) End If End Sub Private Sub ChkUnder_CheckedChanged(sender As Object, e As EventArgs) Handles ChkUnder.CheckedChanged If ChkUnder.Checked Then ' Apply underline style LblDisplay.Font = New Font(LblDisplay.Font, LblDisplay.Font.Style Or FontStyle.Underline) Else ' Remove underline style LblDisplay.Font = New Font(LblDisplay.Font, LblDisplay.Font.Style And Not FontStyle.Underline) End If End Sub End Class
Key Concepts:
- The
CheckedChangedevent responds immediately to user selection - Font styles are applied using bitwise operations (
Orto add,And Notto remove) - This approach retains the original font while modifying only the selected styles
Figure 21.2: Text formatting with checkbox controls
Lesson Summary
In this lesson, you've learned how to effectively implement checkbox controls in VB2019:
Shopping Cart Implementation
Created interactive shopping interfaces with multiple item selection
Text Formatting
Applied multiple font styles using checkbox selections
Event Handling
Utilized CheckedChanged events for real-time UI updates
Practical Applications
Applied checkbox controls to solve real-world interface challenges
Checkbox controls are essential for creating flexible, user-friendly interfaces where multiple selections are needed. In the next lesson, we'll explore radio button controls for mutually exclusive selections.
Next Lesson
Ready to learn about exclusive selections? Continue to Lesson 22: Radio Button Controls.
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.