Combo Boxes
Overview
A combo box is a text field with a list of choices attached to it. You meet them when a website asks for your state, your shirt size or your country. A person can open the list and pick a choice, or start typing, and the list shrinks to the choices that match. In this tutorial, you will add combo boxes to a form, fill them with choices, find out which choice was picked, and make your program react the moment the choice changes.
Goals
You have the following goals for this tutorial:
By the end of this lesson, you can
- Add a combo box to a form with
AddComboBoxand fill it with choices - Read the chosen choice with
GetSelectedIndexandGetSelectedItem - Use a
ComboBoxListenerto react when a person picks a different choice - Check whether typed text matches one of the choices
- Explain how a person using a keyboard or a screen reader uses a combo box
Warm up
Think of a form where you had to pick from a long list, such as a country or a birth year. Did you scroll through the list, or did you type the first few letters? What would make a long list of choices easier to use for someone who can't see the screen, or who can't use a mouse?
Vocabulary
You will be learning about the following vocabulary words:
| Term | Definition |
|---|---|
| Combo box | A text field with a list of choices that opens below it. A person can pick a choice from the list or type in the field. |
| Choice | One of the pieces of text in a combo box's list, such as "Medium" in a list of sizes. |
| Index | A choice's place in the list. The first choice has index 0, the second has index 1, and so on. An index of -1 means nothing is chosen. |
| Placeholder | Words shown in an empty field, such as "Choose a size", that tell a person what to do. They go away once there is text in the field. |
| Narrowing | Showing only the choices that start with the text a person has typed. A combo box ignores upper and lower case when it narrows. |
| Listener | An object that a control tells when something happens. A ComboBoxListener is told when a person picks a different choice. |
New Code to Learn
You will be using the following new pieces of code:
| Quorum Code | Code Example | Explanation |
|---|---|---|
| AddComboBox(text name, Array<text> items) | ComboBox size = page:AddComboBox("Size", sizes) | Adds a combo box to a form page with the choices in the array. The name is what a screen reader says. |
| AddComboBox(text name) | ComboBox color = page:AddComboBox("Color") | Adds a combo box with no choices yet. |
| Add(text item) | color:Add("Blue") | Adds a choice to the end of the list. |
| Select(integer index) | color:Select(0) | Chooses the choice at an index from code and puts its text in the field. Use -1 to choose nothing. |
| GetSelectedIndex() | integer index = size:GetSelectedIndex() | Returns the index of the chosen choice, or -1 if nothing is chosen. |
| GetSelectedItem() | text chosen = size:GetSelectedItem() | Returns the text of the chosen choice, or undefined if nothing is chosen. |
| GetComboBox(text name) | ComboBox size = page:GetComboBox("Size") | Finds a combo box on a page by its name. |
| SetPlaceholder(text value) | size:SetPlaceholder("Choose a size") | Sets the words shown while the field is empty. |
| AddComboBoxListener(ComboBoxListener listener) | size:AddComboBoxListener(listener) | Tells the listener each time a person picks a different choice. |
| ComboBoxChanged(ComboBoxChangedEvent event) | action ComboBoxChanged(ComboBoxChangedEvent event) | The action a listener writes. The event's GetNewIndex, GetOldIndex and GetText say what changed. |
CSTA Standards
This tutorial covers the following CSTA standards:
For teachers · 2026 CSTA K–12 Computer Science Standards
High School · Algorithms & Design
Design a computing technology using human-centered design principles.
Also in 7 other lessons
High School · Data & Analysis
Evaluate different approaches to verifying consistency and compliance with expected data types, values, and ranges.
Also in 3 other lessons
Middle School · Data & Analysis
Analyze options to address a data quality issue.
Also in 2 other lessons
Explore
What is a combo box?
In the Text Fields tutorial, a person could type anything into a field. Sometimes a program wants one answer from a short list instead, such as a shirt size. A combo box does both jobs. It is a text field, so a person can type in it, and it has a list of choices that opens below the field.
In Quorum, the ComboBox class comes from the TextField class. That means
a combo box can do everything a text field can do, such as GetText, plus
keep a list of choices and remember which one is chosen.
A combo box isn't the only way to offer choices. Radio buttons show every choice at once, which is good for two to five choices. A combo box keeps the choices hidden until the list opens, so it fits a long list, such as a list of countries, in a small space.
Making a combo box on a form
The easiest way to make a combo box is on a form, which you met in the Forms tutorial. A page's AddComboBox action
makes a combo box, adds it to the page and returns it so that you can change
it. There are two versions:
AddComboBox(name, items)takes anArrayof text with the choices in order;AddComboBox(name)makes an empty combo box, and you add choices with itsAddaction.
The name is what a screen reader says when the combo box gets the focus. A
label added with AddLabel just before the combo box is shown beside it, the
same way a label sits beside a text field.
The program below makes a T-shirt order form with two combo boxes and a button.
use Libraries.Interface.Forms.Form
use Libraries.Interface.Forms.Page
use Libraries.Interface.Controls.ComboBox
use Libraries.Interface.Controls.Button
use Libraries.Containers.Array
Form form
Page page = form:GetMainPage()
page:SetTitle("Order a T-Shirt")
Array<text> sizes
sizes:Add("Small")
sizes:Add("Medium")
sizes:Add("Large")
sizes:Add("Extra large")
page:AddLabel("Size")
ComboBox sizeBox = page:AddComboBox("Size", sizes)
sizeBox:SetDescription("The size of your shirt.")
sizeBox:SetPlaceholder("Choose a size")
page:AddLabel("Color")
ComboBox colorBox = page:AddComboBox("Color")
colorBox:SetDescription("The color of your shirt.")
colorBox:Add("Black")
colorBox:Add("Blue")
colorBox:Add("Gray")
colorBox:Add("Green")
colorBox:Add("White")
colorBox:Select(0)
Button order = page:AddButton("Place order")
order:SetDescription("Shows the shirt you picked.")
OrderBehavior behavior
order:SetBehavior(behavior)
form:Display()
The size box starts empty, so it shows its placeholder, "Choose a size." The
color box starts with "Black" chosen, because Select(0) chose the choice at
index 0. Remember that indexes start at 0: "Black" is 0, "Blue" is 1, and so
on.
Picking a choice
A person can pick a choice in several ways:
- With the mouse: click the field to open the list, then click a choice. Clicking the field again, or anywhere outside the list, closes it.
- With the arrow keys: press Down to open the list, then Down and Up to move through it. The highlight moves, and the field's text stays the same until you choose. Press Enter to choose it.
- By typing: type the first letters of a choice. The list opens and shows only the choices that start with what you typed, ignoring upper and lower case. Typing "g" in the color box leaves "Gray" and "Green."
Here are all of a combo box's keys:
| Key | When the list is closed | When the list is open |
|---|---|---|
| Typing | Opens the list with only the matching choices | Narrows the list |
| Down or Alt+Down | Opens the list | Moves to the next choice |
| Up | Opens the list | Moves to the previous choice |
| Home, End | Moves to the start or end of the text | Moves to the first or last choice |
| Enter | Does nothing in the combo box | Chooses the highlighted choice and closes the list |
| Escape | Right after the list closed, puts back the text the field had when it opened | Closes the list |
| Tab | Moves to the next control | Closes the list, keeps the text, and moves on |
When the focus leaves a combo box, it looks at the text in the field. If the text is exactly one of the choices, that choice is chosen. If it isn't, nothing is chosen. So typing "Medium" and pressing Tab chooses "Medium."
Finding out which choice was picked
A combo box has two actions for reading the choice:
GetSelectedIndexreturns the chosen choice's index, or -1 if nothing is chosen;GetSelectedItemreturns the chosen choice's text, orundefinedif nothing is chosen.
The "Place order" button uses both. Make a new class in a file named OrderBehavior.quorum. Like the behaviors in the Checkboxes tutorial, it comes from FormBehavior, which gives it the GetPage action, so it can find each combo
box by name with GetComboBox.
use Libraries.Interface.Forms.FormBehavior
use Libraries.Interface.Forms.Page
use Libraries.Interface.Events.BehaviorEvent
use Libraries.Interface.Controls.ComboBox
class OrderBehavior is FormBehavior
action Run(BehaviorEvent event)
Page page = GetPage()
ComboBox sizeBox = page:GetComboBox("Size")
ComboBox colorBox = page:GetComboBox("Color")
if sizeBox = undefined
return now
end
if colorBox = undefined
return now
end
if sizeBox:GetSelectedIndex() = -1
output "Please choose a size from the list."
elseif colorBox:GetSelectedIndex() = -1
output "Please choose a color from the list."
else
output "You ordered a " + colorBox:GetSelectedItem() + " shirt, size " + sizeBox:GetSelectedItem() + "."
end
end
end
Run the program. Choose "Large" and "Blue" and press "Place order." The output says you ordered a Blue shirt, size Large. Now type "Huge" in the size box and press "Place order." "Huge" isn't one of the choices, so nothing is chosen, and the program asks for a size from the list.
Checking typed text
Because a person can type anything into a combo box, the text in the field isn't always one of the choices. That is a data quality problem: a program that expects "Small," "Medium," "Large" or "Extra large" might get "Huge," "large" or "L." A combo box gives you two ways to tell the difference:
GetText, fromTextField, returns whatever is in the field;GetSelectedIndexreturns -1 when that text isn't exactly one of the choices.
The order behavior above chose one way to handle a bad answer: it refuses the order and asks again. Other programs might take the closest choice, or keep the new text as a new choice. Which one is best depends on what the answer is used for.
Reacting when the choice changes
Sometimes a program should react the moment a person picks a choice, without waiting for a button. A combo box tells its listeners each time a person picks a different choice: by clicking it, by pressing Enter, or by leaving the field with text that chooses a different one.
A listener is a class that comes from ComboBoxListener and writes the ComboBoxChanged action. The action gets a ComboBoxChangedEvent, which
has:
GetNewIndex, the index of the new choice, or -1 for none;GetOldIndex, the index before the change;GetText, the new choice's text, or the field's text when nothing is chosen;GetComboBox, the combo box that changed.
Make a class in a file named SizeListener.quorum. It writes the new size
into a label on the page.
use Libraries.Interface.Events.ComboBoxListener
use Libraries.Interface.Events.ComboBoxChangedEvent
use Libraries.Game.Graphics.Label
class SizeListener is ComboBoxListener
Label summary = undefined
action SetLabel(Label newLabel)
summary = newLabel
end
action ComboBoxChanged(ComboBoxChangedEvent event)
if summary = undefined
return now
end
if event:GetNewIndex() = -1
summary:SetText(event:GetText() + " is not one of the sizes.")
else
summary:SetText("Your size: " + event:GetText())
end
end
end
Now give the listener to a combo box:
use Libraries.Interface.Forms.Form
use Libraries.Interface.Forms.Page
use Libraries.Interface.Controls.ComboBox
use Libraries.Game.Graphics.Label
use Libraries.Containers.Array
Form form
Page page = form:GetMainPage()
page:SetTitle("Pick a Size")
Array<text> sizes
sizes:Add("Small")
sizes:Add("Medium")
sizes:Add("Large")
sizes:Add("Extra large")
page:AddLabel("Size")
ComboBox sizeBox = page:AddComboBox("Size", sizes)
sizeBox:SetDescription("The size of your shirt.")
sizeBox:SetPlaceholder("Choose a size")
Label summary = page:AddLabel("Summary", "No size chosen yet.")
SizeListener listener
listener:SetLabel(summary)
sizeBox:AddComboBoxListener(listener)
form:Display()
Pick a size and notice how the label changes. Then type "Huge" and press Tab. The label says "Huge is not one of the sizes."
A program's own Select doesn't tell listeners, because a person didn't
make the change. If you add sizeBox:Select(1) to the program, the label still
says "No size chosen yet."
What a screen reader says
When a combo box gets the focus, a screen reader says its name, that it is a combo box, and the text in the field. Then it reads the description. As the person presses Down and Up, the focus stays in the field, but the screen reader is pointed at the highlighted choice, so it reads each choice as the person moves to it. The exact words depend on the screen reader.
This is why the name matters. A combo box named "Combo Box," which is the name it gets if you never set one, tells a screen reader user nothing. A combo box named "Size" says exactly what it asks for.
Colors
A combo box takes its colors from the current theme, so it changes in dark mode too. The Themes and Dark Mode tutorial shows how to change them with the theme keys "ComboBox" and "ComboBoxChoice."
Engage
In this activity, you build a form for planning a made-up class field trip. The form needs at least two combo boxes: one for the place to visit, with at least eight choices, and one for the day of the week. Give each combo box a label, a clear name, a description and a placeholder. Add a "Submit" button. When it is pressed, output the plan if both boxes have a choice. If a box has text that isn't one of the choices, output a message that names the box and asks for a choice from its list.
Then decide what your form should do when someone types a place that isn't
on the list. Should it refuse it, or add it as a new choice with Add? Think
about two made-up users: a teacher who must approve every place, and a
student who knows a great museum that isn't listed. Write one sentence that
explains your choice and who it helps.
When it works, test it with this self-check list, using only the keyboard: Tab, Down, Up, Enter, Escape and typing.
- Can you reach both combo boxes and the button, and open, move through and close each list?
- Does typing a few letters narrow the list?
- Does the message name the right box when you type something not on the list?
- Does a person who has never seen the form know what each placeholder asks for?
- Turn on a screen reader (CTRL + ALT + Z on a Chromebook): does it report each combo box's name and choice?
Fix anything that fails, then rewrite one name, description or placeholder so it is clearer.
Wrap up
A combo box is a text field with a list of choices. You can read the choice
with GetSelectedIndex and GetSelectedItem, or react right away with a ComboBoxListener. Because a person can also type, a good program checks
for -1 and decides what to do with text that isn't a choice. Think back to
the warm up: which long lists on the forms you use would be easier to fill in
by typing? In the next tutorial, Sliders, you
will let a person choose a number instead of a word.