Checkboxes
Overview
A checkbox is a small box that a person can turn on or off. You find them everywhere: "Remember me," "Send me the newsletter," or a list of pizza toppings. Each checkbox answers one yes-or-no question, and a person can check as many boxes as they want. In this tutorial, you will add checkboxes to a page, find out which ones are checked, and make your program react the moment a box changes.
Goals
You have the following goals for this tutorial:
By the end of this lesson, you can
- Add checkboxes to a page and give each one a clear name and description
- Read whether a checkbox is checked with
GetToggleState - Use a behavior's
RunandDisposeactions to react when a box is checked or unchecked - Explain how a person using a keyboard or a screen reader uses a checkbox, and how a box that starts checked can affect people
Warm up
Think about the last time you signed up for something online. Which choices were checkboxes? Were any of them already checked for you? Who do you think benefits when a box starts out checked: the person signing up or the company?
Vocabulary
You will be learning about the following vocabulary words:
| Term | Definition |
|---|---|
| Checkbox | A control with a small box that a person turns on (checked) or off (unchecked). Each checkbox is its own yes-or-no choice. |
| Toggle state | Whether a toggle button is on (true) or off (false). For a checkbox, true means checked. |
| ToggleButton | The Quorum class that every on/off button comes from. Checkbox and RadioButton are both kinds of ToggleButton. |
| Behavior | A class whose Run action is called when a person uses a control. A checkbox also calls the behavior's Dispose action when it is unchecked. |
| Description | Extra text about a control that a screen reader reads after its name. You set it with SetDescription. |
| Default | The value a control starts with before a person changes it, such as a box that starts checked. |
New Code to Learn
You will be using the following new pieces of code:
| Quorum Code | Code Example | Explanation |
|---|---|---|
| AddCheckbox(text name) | Checkbox box = page:AddCheckbox("Mushrooms") | Adds a checkbox to a page. The name is the label a person sees and the name a screen reader says. |
| GetToggleState() | boolean checked = box:GetToggleState() | Returns true if the checkbox is checked and false if it is not. |
| SetToggleState(boolean state) | box:SetToggleState(true) | Checks (true) or unchecks (false) the checkbox from code. |
| GetCheckbox(text name) | Checkbox box = page:GetCheckbox("Mushrooms") | Finds a checkbox on a page by its name. |
| SetDescription(text description) | box:SetDescription("Adds mushrooms to your pizza.") | Gives the checkbox extra text for screen readers. |
| SetBehavior(Behavior behavior) | box:SetBehavior(behavior) | Runs the behavior's Run action when the box is checked and its Dispose action when the box is unchecked. |
CSTA Standards
This tutorial covers the following CSTA standards:
For teachers · 2026 CSTA K–12 Computer Science Standards
High School · Computing & Society
Evaluate how human choices in using, designing, deploying, and regulating computing technologies have risks, benefits, and long-term impacts.
Also in 1 other lesson
Middle School · Computing & Society
Analyze how the decisions humans make when using computing technologies have ethical and social consequences.
Also in 2 other lessons
Specialty I · Software Development
Refine a user interface design using accessibility and responsive design tools.
Also in 13 other lessons
Middle School · Data & Analysis
Explain the benefits and risks of allowing personal data and metadata to be collected and used in datasets, including issues of data ownership, privacy, and sovereignty.
Also in 1 other lesson
Explore
What is a checkbox?
In the Buttons tutorial, a button did one thing each time a person pressed it. A checkbox is a special kind of button called a toggle button. A toggle button remembers whether it is on or off. Each time a person activates it, it flips to the other state. This on-or-off value is called its toggle state.
In Quorum, the Checkbox class comes from the ToggleButton class, and ToggleButton comes from Button. That means a checkbox can do everything a
button can do, plus remember its state. The radio buttons in the next tutorial
come from ToggleButton too.
Checkboxes are independent. Checking one box never unchecks another. If you want a person to pick exactly one choice, use radio buttons instead.
Making checkboxes on a page
A page's AddCheckbox action makes a checkbox, adds it to the page and
returns it so that you can change it. The text you pass in is the
checkbox's name. It is the label a person sees beside the box and the name a
screen reader says. Each checkbox gets its own row on the page.
The program below makes a pizza order form with three toppings and a button. Each checkbox also gets a description. A screen reader reads the description after the name, so it is a good place for a short hint.
use Libraries.Interface.Forms.Form
use Libraries.Interface.Forms.Page
use Libraries.Interface.Controls.Checkbox
use Libraries.Interface.Controls.Button
Form form
Page page = form:GetMainPage()
page:SetTitle("Build a Pizza")
Checkbox cheese = page:AddCheckbox("Extra cheese")
cheese:SetDescription("Adds extra cheese to your pizza.")
Checkbox mushrooms = page:AddCheckbox("Mushrooms")
mushrooms:SetDescription("Adds mushrooms to your pizza.")
Checkbox peppers = page:AddCheckbox("Peppers")
peppers:SetDescription("Adds green peppers to your pizza.")
Button order = page:AddButton("Place order")
order:SetDescription("Shows the toppings you picked.")
page:AddLabel("Order", "You haven't ordered yet.")
OrderBehavior behavior
order:SetBehavior(behavior)
form:Display()
The page sets the focus order for you, so you can already move between the boxes with Tab and Shift+Tab. Clicking a box, or pressing Space while it has the focus, checks or unchecks it.
On a phone or tablet:
- With touch: tap a box to check it. Tap it again to uncheck it.
- With VoiceOver (iPhone and iPad) or TalkBack (Android) on: swipe right to move to the next control and left to move to the previous one. When the screen reader reaches a box, it reads the box's name and whether it is checked. Double-tap anywhere on the screen to check or uncheck the box it is on.
Finding out which boxes are checked
A checkbox's GetToggleState action returns true if the box is checked. The "Place order" button uses this to list the toppings. Make a new class in a
file named OrderBehavior.quorum. It comes from FormBehavior, which gives it
the GetPage action, so it can find each checkbox by name with GetCheckbox.
use Libraries.Interface.Forms.FormBehavior
use Libraries.Interface.Forms.Page
use Libraries.Interface.Events.BehaviorEvent
use Libraries.Interface.Controls.Checkbox
use Libraries.Game.Graphics.Label
class OrderBehavior is FormBehavior
action Run(BehaviorEvent event)
Page page = GetPage()
text toppings = ""
toppings = toppings + ToppingIfChecked(page, "Extra cheese")
toppings = toppings + ToppingIfChecked(page, "Mushrooms")
toppings = toppings + ToppingIfChecked(page, "Peppers")
Label order = page:GetLabel("Order")
if toppings = ""
order:SetText("You ordered a plain pizza.")
else
order:SetText("Your pizza has:" + toppings)
end
end
action ToppingIfChecked(Page page, text name) returns text
Checkbox box = page:GetCheckbox(name)
if box = undefined
return ""
end
if box:GetToggleState()
return " " + name
end
return ""
end
end
Run the program, check two toppings and press "Place order." The label below the button lists the two toppings you picked. A screen reader user can press Tab once more to reach it.
Reacting when a box changes
Sometimes a program should react the moment a box changes, not when another button is pressed. A checkbox can have a behavior, just like a button, but it uses two of the behavior's actions:
- when the box is checked, the checkbox calls the behavior's
Runaction; - when the box is unchecked, it calls the behavior's
Disposeaction.
Make a class in a file named ReportBehavior.quorum. Run gets the checkbox
from the event and saves its name, so that Dispose can use the name later.
use Libraries.Interface.Behaviors.Behavior
use Libraries.Interface.Events.BehaviorEvent
class ReportBehavior is Behavior
text name = "The checkbox"
action Run(BehaviorEvent event)
name = event:GetItem():GetName()
output name + " is now on."
end
action Dispose
output name + " is now off."
end
end
Now give the behavior to a checkbox. This program adds one setting and starts
it checked with SetToggleState(true):
use Libraries.Interface.Forms.Form
use Libraries.Interface.Forms.Page
use Libraries.Interface.Controls.Checkbox
Form form
Page page = form:GetMainPage()
page:SetTitle("Settings")
Checkbox newsletter = page:AddCheckbox("Send me the newsletter")
newsletter:SetDescription("You will get one email each week.")
ReportBehavior behavior
newsletter:SetBehavior(behavior)
newsletter:SetToggleState(true)
form:Display()
Calling SetToggleState(true) after SetBehavior runs the behavior right
away, so the program starts by writing that the newsletter is on. If you set
the state before you set the behavior, nothing is written at the start. Try
both.
Who chose the default?
The value a control starts with is called its default. Most people never change a default, so the person who sets it makes a choice for many other people. A newsletter box that starts checked signs up everyone who doesn't notice it. That may be good for the company and bad for the person.
When you write a form, start each box unchecked unless checking it is clearly what the person wants. If a box must start checked, say so in its name or description, so a screen reader user hears it too.
What a screen reader says
A screen reader says a checkbox's name, that it is a checkbox, and whether it is checked. Then it may read the description. The exact words depend on the screen reader. When the person presses Space, the screen reader says the new state.
This is why the name matters. A box named "Option 1" tells a screen reader user nothing. A box named "Extra cheese" says exactly what it does.
Grouping checkboxes
A long list of checkboxes is easier to read with a heading. As in the Groupings tutorial, put them in a grouping with a title, and the page draws them together, under the title, as one card:
use Libraries.Interface.Forms.Form
use Libraries.Interface.Forms.Page
use Libraries.Interface.Forms.Grouping
use Libraries.Interface.Controls.Checkbox
Form form
Page page = form:GetMainPage()
page:SetTitle("Build a Pizza")
Grouping toppings = page:AddGrouping("Toppings")
toppings:SetTitle("Toppings")
Checkbox cheese = toppings:AddCheckbox("Extra cheese")
cheese:SetDescription("Adds extra cheese to your pizza.")
Checkbox mushrooms = toppings:AddCheckbox("Mushrooms")
mushrooms:SetDescription("Adds mushrooms to your pizza.")
form:Display()
The checkboxes in the grouping are still independent: checking one never
unchecks another. page:GetCheckbox finds them inside the grouping too, so OrderBehavior works without changes. This is the same kind of card you
made for the Pet Sitter app.
Colors
The colors of the box and the checkmark come from the current theme, so they match the rest of the program and change in dark mode. You don't set them on the checkbox itself. The Themes and Dark Mode tutorial shows how to change them.
Engage
In this activity, you build a sign-up form for a made-up school club. The goal is to use checkboxes for choices where more than one answer can be picked, to choose defaults carefully, and to give clear feedback when the form is submitted. The form needs at least four checkboxes, for example one for each day a person can meet, and a "Sign up" button. Give every checkbox a clear name and a description. When the button is pressed, show the days that were checked in a label. If no days were checked, show a message that asks the person to pick at least one.
Add one more checkbox: "Share my email with other club members." Decide whether it should start checked or unchecked. To decide, think about two made-up members: Sam wants to meet other members outside the club, and Priya doesn't want her email shared with people she hasn't met. Write your choice and your reasons in a comment in your code. Who is helped and who could be harmed by each choice?
When it works, test it with this self-check list, using only the keyboard: Tab, Shift+Tab and Space.
- Can you reach every checkbox and the button, in the order they appear?
- Does Space check and uncheck each box?
- Would each name make sense if it were the only thing you knew about the box?
- Does the label show the right days, and the message when no day is checked?
- If you have a screen reader, turn it on: does it report each box's name and whether it is checked?
Fix anything that fails, then pick one name or description and rewrite it so it is clearer.
Wrap up
A checkbox is a toggle button that stores one yes-or-no answer. You can read
its state with GetToggleState when you need it, or react right away with a
behavior's Run and Dispose actions. Think back to the warm up: when you
write a form, which boxes should start checked, and why?