Lesson 21

Checkbox

In this lesson, you will learn how to use the CheckBox control in Visual Basic 2015. A checkbox allows the user to select one or more items independently. This makes it different from controls such as radio buttons, where only one option is normally selected at a time.

Lesson focus:

Checkboxes are useful whenever the user needs to choose several options at the same time, such as selecting products in a shopping cart or applying text styles like bold, italic, and underline.

Lesson Overview

Lesson21
TopicCheckBox Control
Main FocusMultiple Selection
Key SkillChecked State and Events
Next StepRadio Button
21.1 Checkbox Basics
21.2 Shopping Cart
21.3 Price Selection
21.4 Font Styling

What Is a Checkbox?

The CheckBox control allows the user to turn an option on or off by ticking or clearing a box. In many applications, several checkboxes can be selected at once.

A common real-world example is the Font dialog box in word processors such as Microsoft Word, where the user may choose several effects such as underline, superscript, or small caps at the same time.

In Visual Basic 2015, checkboxes are also useful for shopping carts, option panels, questionnaires, settings pages, and formatting tools.

Shopping Cart with Checkboxes

In this example, a simple shopping cart is created using six checkboxes, a label for the total amount, and two buttons. Each item has a fixed price declared by using the Const keyword.

If a checkbox is ticked, its Checked property is True. The program then adds the corresponding item price to the total by using the += operator. For example:

sum += BN

means the same as:

sum = sum + BN

Finally, the total is displayed in currency format by using ToString("c").

The Shopping Cart Program

Public Class Form1

Private Sub BtnCal_Click(sender As Object, e As EventArgs) Handles BtnCal.Click
    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

    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

    LblTotal.Text = sum.ToString("c")
End Sub

Private Sub BtnReset_Click(sender As Object, e As EventArgs) Handles BtnReset.Click
    CheckBox1.Checked = False
    CheckBox2.Checked = False
    CheckBox3.Checked = False
    CheckBox4.Checked = False
    CheckBox5.Checked = False
    CheckBox6.Checked = False
End Sub

End Class

The Reset button clears all selections by setting each checkbox to False.

VB2015 Figure 21.1 Shopping cart interface

Figure 21.1: Shopping cart using checkboxes

A Simpler Price Selection Example

The next example uses three checkboxes to select item sizes such as large, medium, and small. Each selected checkbox contributes its fixed value to the total.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Const large As Integer = 10.0
    Const medium As Integer = 8
    Const small As Integer = 5
    Dim sum As Integer

    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

    Label5.Text = sum.ToString("c")

End Sub

This example shows that the same checkbox logic can be reused in smaller and simpler selection systems.

Formatting Label Text with Checkboxes

Checkboxes can also be used for text formatting. In this example, three checkboxes represent Bold, Italic, and Underline. Whenever the user checks or unchecks an option, the label’s font style changes accordingly.

This program uses the CheckedChanged event, which responds whenever the check state changes.

The Font Styling Program

Public Class Form1

Private Sub ChkBold_CheckedChanged(sender As Object, e As EventArgs) Handles ChkBold.CheckedChanged
    If ChkBold.Checked Then
        LblDisplay.Font = New Font(LblDisplay.Font, LblDisplay.Font.Style Or FontStyle.Bold)
    Else
        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
        LblDisplay.Font = New Font(LblDisplay.Font, LblDisplay.Font.Style Or FontStyle.Italic)
    Else
        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
        LblDisplay.Font = New Font(LblDisplay.Font, LblDisplay.Font.Style Or FontStyle.Underline)
    Else
        LblDisplay.Font = New Font(LblDisplay.Font, LblDisplay.Font.Style And Not FontStyle.Underline)
    End If
End Sub

End Class

The statement below keeps the same font family and size, but adds italic formatting:

LblDisplay.Font = New Font(LblDisplay.Font, LblDisplay.Font.Style Or FontStyle.Italic)

The next statement removes italic formatting while preserving the rest of the label’s original font settings:

LblDisplay.Font = New Font(LblDisplay.Font, LblDisplay.Font.Style And Not FontStyle.Italic)

The same logic is used for bold and underline.

VB2015 Figure 21.2 Font styling with checkboxes

Figure 21.2: Output of the checkbox-based font styling example

Why Checkboxes Are Useful

Core takeaway:

Checkboxes are ideal when the user should be allowed to choose multiple options independently. They are simple to use, highly flexible, and useful in shopping carts, settings screens, option panels, surveys, and text formatting tools.

Build on This Foundation

Continue to VB2026

After learning the basics of checkbox controls in VB2015, move to the newest VB2026 tutorial for a more modern VB.NET learning path.

Explore VB2026 β†’

Visual Basic Programming

Visual Basic Programming

Use this Top Release book to reinforce your tutorial learning with a more structured guide.

Exercise Questions

  1. What is the main difference between a checkbox and a radio button?
  2. Why is the Checked property important when working with checkboxes?
  3. Write a short VB2015 example that uses two checkboxes to add two optional charges to a total price.

Go to Lesson 22

In the next lesson, you will learn how to work with the Radio Button control in Visual Basic 2015.