Lesson 21 · CheckBox Control

The CheckBox Control

Use Windows Forms checkboxes to capture independent choices, respond to changes, and handle an indeterminate state in Visual Basic 2026.

Key Takeaway: Read Checked for a two-state choice and handle CheckedChanged to update the form when that Boolean value changes. For a three-state choice, use CheckState and CheckStateChanged.

21.1 Adding a CheckBox in the Designer

Create a Visual Basic Windows Forms App in Visual Studio 2026. Open Form1 in the Designer, drag a CheckBox onto the form, and set its (Name) to chkEmail and its Text to Receive email updates. Add a Label named lblStatus and set its initial text to Email updates are off. Leave Checked set to False.


21.2 CheckBox Properties and Events

Text and AutoSize
Set the caption and let the control size itself to fit that caption.
Checked and CheckedChanged
Read the Boolean value and respond whenever it changes, including when code sets it.
CheckState and CheckStateChanged
Read and respond to changes among Unchecked, Checked, and Indeterminate.
ThreeState
Set this to True to let the user cycle through all three states.

21.3 Responding to CheckedChanged

Add this method inside Public Class Form1 in Form1.vb. Run the app and toggle the checkbox. With the checkbox focused, the Space key also toggles it.

Form1.vb — Visual Basic 2026
Private Sub EmailOptionChanged(sender As Object, e As EventArgs) Handles chkEmail.CheckedChanged
    lblStatus.Text = If(chkEmail.Checked, "Email updates are on.", "Email updates are off.")
End Sub
Try It — Simulation 21.1: Email Preference
Simulation 21.1: Email Preference

Email updates are off.


21.4 Grouping Independent Options

A GroupBox gives related controls a shared heading. For a lunch order, customers can select cheese, salad, both, or neither. Place one CheckBox per extra inside the GroupBox. Each option keeps its own value. Lesson 22 uses RadioButton controls for choices such as selecting a single drink size.


21.5 Working with Three States

In a separate form, add a CheckBox called chkReview, set its Text to Review status, and set ThreeState to True. Add a Label called lblReview with the initial text Review declined. Here the third state means a decision is pending. The Boolean Checked is True for both Checked and Indeterminate, so use CheckState to distinguish them.

Form1.vb — Visual Basic 2026
Private Sub ReviewStateChanged(sender As Object, e As EventArgs) Handles chkReview.CheckStateChanged
    Select Case chkReview.CheckState
        Case CheckState.Unchecked
            lblReview.Text = "Review declined."
        Case CheckState.Checked
            lblReview.Text = "Review approved."
        Case CheckState.Indeterminate
            lblReview.Text = "Review pending."
    End Select
End Sub
Try It — Simulation 21.2: Three-State Review
Simulation 21.2: Three-State Review

Checked = False. Review declined.


21.6 Practical Project: Lunch Order

Create a fresh Visual Basic Windows Forms App. Add a GroupBox with Text set to Lunch extras, then add the controls below. Leave both boxes unchecked. Paste the full class into Form1.vb. This project uses the F2 format specifier from Lesson 20 to show two decimal places.

Form1.vb — Visual Basic 2026
Public Class Form1
    Private Const BasePrice As Decimal = 12D
    Private Const CheesePrice As Decimal = 2D
    Private Const SaladPrice As Decimal = 4.5D

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        UpdateTotal()
    End Sub

    Private Sub ExtrasChanged(sender As Object, e As EventArgs) Handles chkCheese.CheckedChanged, chkSalad.CheckedChanged
        UpdateTotal()
    End Sub

    Private Sub UpdateTotal()
        Dim total As Decimal = BasePrice
        If chkCheese.Checked Then total += CheesePrice
        If chkSalad.Checked Then total += SaladPrice

        lblTotal.Text = $"Total: RM {total:F2}"
    End Sub
End Class
Try It — Simulation 21.3: Lunch Order
Simulation 21.3: Lunch Order

Base lunch — RM 12.00

Total: RM 12.00


21.7 Testing the Form

Run the app and test all four combinations. With neither extra selected, expect RM 12.00. Cheese alone gives RM 14.00, salad alone gives RM 16.50, and both give RM 18.50. Clear both boxes and check that the total returns to RM 12.00. If the total fails to update, check the control names and confirm the shared event handler lists both controls after Handles.


21.8 GitHub Copilot — Extending the Lunch Order

GitHub Copilot — Add a Reset Button
Try this prompt: Add a Click handler for a Windows Forms button named btnReset. Clear chkCheese and chkSalad. Use the existing CheckedChanged handler to recalculate the total.

Add btnReset in the Designer before using the suggested code. Test Reset when both extras are selected and again when both are already clear.


Lesson Summary

  • Use Checked to read an independent on/off option.
  • Use CheckedChanged to update a form when a Boolean selection changes.
  • Use ThreeState, CheckState, and CheckStateChanged to distinguish an indeterminate choice.
  • Share an event handler when several options update the same result.

Exercises

Exercise 21.1 — Preferences

Add a second checkbox for SMS updates. Show whether email, SMS, both, or neither is selected.

Exercise 21.2 — Extra Side Dish

Add a soup option priced at RM 3.50. Update the shared handler and confirm that selecting all three extras gives RM 22.00.

Exercise 21.3 — Pending Review

Add a button that sets chkReview.CheckState to CheckState.Indeterminate. Confirm that the label updates when the state changes.

Next: Lesson 22 — Radio Button Control

Use radio buttons when the user must choose a single option from a group.

Continue »

Related Resources


Featured Books

Visual Basic 2026 Made Easy — Second Edition

Visual Basic 2026 Made Easy — Second Edition

by Dr. Liew Voon Kiong

Learn VB.NET with Visual Studio 2026 and .NET 10: Windows Forms, Databases, LINQ, Async Programming, Practical Projects, and GitHub Source Code

View on Amazon → Kindle Edition →
Visual Basic 2026 Made Easy

Visual Basic 2026 Made Easy

by Dr. Liew Voon Kiong

48 fully-explained code examples spanning VB6 through VB.NET — ideal for building real-world applications.

View on Amazon → Kindle Edition →
Advanced Visual Basic 2026 Programming, Volume 1

Advanced Visual Basic 2026 Programming, Volume 1

by Dr. Liew Voon Kiong

Professional VB.NET Engineering, Object Modelling, Generics, Events, Design Patterns, and LINQ

View on Amazon → Kindle Edition →