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.

Form1.vb
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
Shopping Cart Interface

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.

Form1.vb
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.

Font Style Formatting

This example demonstrates how to use checkboxes to apply multiple font styles to a label:

Form1.vb
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 CheckedChanged event responds immediately to user selection
  • Font styles are applied using bitwise operations (Or to add, And Not to remove)
  • This approach retains the original font while modifying only the selected styles
Text Formatting Interface

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

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