Lists

Overview

A list shows a group of choices, one on each line. A person moves through the choices and picks one. You use lists all the time: a playlist, a list of files, or the contacts on a phone. In this tutorial, you will build a shopping list on a page. You will add items to it, react when a person picks an item, and change the list while the program runs. You will also make sure the list works with a keyboard and a screen reader.

Goals

You have the following goals for this tutorial:

By the end of this lesson, you can

  • Add a list to a page with AddList, and add text items to it
  • Run a behavior when a person activates an item in the list
  • Add, remove and read items from code while the program runs
  • Give a list a name and description so a screen reader can describe it

Warm up

Think about a list you used today, like a menu of songs or a list of messages. How did you move through it? Did you use a mouse, your finger, or the keyboard? What happened when you picked something? Write down two things a list must do so that anyone can use it, including a person who can't see the screen.

Vocabulary

You will be learning about the following vocabulary words:

Vocabulary
TermDefinition
ListA control that shows a group of text items, one per line, so a person can pick one.
List itemOne entry in a list. In Quorum, each entry is a ListItem object.
PositionWhere an item is in the list. The top item is at position 0, the next at position 1, and so on.
SelectionThe item a person is on right now. Moving with the arrow keys changes the selection.
ActivateTo choose an item, for example by double-clicking it or pressing the Space key on it.
BehaviorA class with a Run action. Quorum runs it when something happens, like an item being activated.
CastTelling Quorum to treat an object as a more specific type, for example treating an Item as a ListItem.

New Code to Learn

You will be using the following new pieces of code:

New Code to Learn
Quorum CodeCode ExampleExplanation
use Libraries.Interface.Controls.Listuse Libraries.Interface.Controls.ListLets your program use the List control. This is not the same List as the one in Libraries.Containers.
AddList(text name)List shoppingList = page:AddList("Shopping list")Adds a list to a page. The name is what a screen reader calls the list.
GetList(text name)List shoppingList = page:GetList("Shopping list")Finds a list on a page by its name.
Add(text entry)shoppingList:Add("Milk")Adds a line of text to the end of the list.
Add(integer position, text entry)shoppingList:Add(0, "Bread")Adds a line of text at a position. Position 0 is the top.
Remove(text entry)ListItem removed = shoppingList:Remove("Ham")Removes the first item with that text and returns it.
Get(integer index)text first = shoppingList:Get(0)Returns the text of the item at a position.
GetSize()integer count = shoppingList:GetSize()Returns how many items are in the list.
Select(integer index)shoppingList:Select(0)Moves the selection to the item at a position.
SetItemActivationBehavior(Behavior behavior)shoppingList:SetItemActivationBehavior(behavior)Sets the behavior that runs when any item is activated.
GetText()text words = item:GetText()Returns the text of a ListItem.

CSTA Standards

This tutorial covers the following CSTA standards:

For teachers · 2026 CSTA K–12 Computer Science Standards

HS-PRO-VD-16

High School · Programming

Create a program that uses appropriate data structures to store, access, and manipulate data.

Also in 1 other lesson

MS-PRO-VD-16

Middle School · Programming

Use variables of multiple data types to store, access, and manipulate data within a program.

Also in 6 other lessons

S1-SWD-UX-04

Specialty I · Software Development

Refine a user interface design using accessibility and responsive design tools.

Also in 13 other lessons

Every standard, and the lessons that cover it

Explore

What is a list?

A list is a control that shows a group of text items, one per line. A person can move through the items and activate one, which means choosing it. Each line in a Quorum list is a ListItem, but for most programs you only need to give the list some text. Quorum makes the ListItem for you.

Quorum has two classes named List. The one in this tutorial is a control in a user interface, and it lives in Libraries.Interface.Controls. The other one is a container for storing data, and it lives in Libraries.Containers. Make sure your use line names the right one:

use Libraries.Interface.Controls.List

Making a list

To start, make a new Form project in Quorum Studio. Replace the code in Main.quorum with the program below. It adds a list with four items to a page and selects the first item.

use Libraries.Interface.Forms.Form
use Libraries.Interface.Forms.Page
use Libraries.Interface.Controls.List

Form form
Page page = form:GetMainPage()
page:SetTitle("Shopping")

List shoppingList = page:AddList("Shopping list")
shoppingList:SetDescription("Things to buy at the store")

shoppingList:Add("Milk")
shoppingList:Add("Eggs")
shoppingList:Add("Ham")
shoppingList:Add("Cheese")
shoppingList:Select(0)

form:Display()

Here is what each part does:

  • page:AddList("Shopping list") adds a list to the page. The page decides where it goes and how big it is. The name, "Shopping list", is what a screen reader calls the list, instead of just "List".
  • SetDescription tells a screen reader a little more about the list.
  • Add puts a line of text at the end of the list.
  • Select(0) selects the top item, so the person starts on Milk.

The list is the only control on the page, so the form gives it the keyboard focus when the program starts. A person can use it right away without a mouse.

Using a list with the keyboard

Run the program. You can use the list with the mouse or with these keys:

  • Up Arrow and Down Arrow move the selection one item.
  • Home and End jump to the first or last item.
  • Typing a letter, like C, jumps to the next item that starts with it. This type of operation is often called "first letter navigation."
  • Space activates the selected item. Double-clicking an item also activates it.

Right now, activating an item does nothing. Next, you will change that.

Doing something when an item is activated

A behavior is a class with a Run action. A list can have one item activation behavior. Quorum runs it whenever any item in the list is activated.

Add a new class to your project and name it ListBehavior.quorum. Put this code in it:

use Libraries.Interface.Behaviors.Behavior
use Libraries.Interface.Events.BehaviorEvent
use Libraries.Interface.Controls.ListItem

class ListBehavior is Behavior
    action Run(BehaviorEvent event)
        ListItem item = cast(ListItem, event:GetItem())
        text message = "I need to get " + item:GetText()

        output message
    end
end

event:GetItem() returns the item that was activated. It comes back as a general Item, so the cast tells Quorum to treat it as a ListItem. Then GetText gives you the words on that line.

Now go back to Main.quorum. Add these two lines just before form:Display():

ListBehavior behavior
shoppingList:SetItemActivationBehavior(behavior)

Run the program again. When you activate "Eggs", the program writes "I need to get Eggs" in Quorum Studio's output window.

Changing a list while the program runs

You can change a list while your program runs, for example when a person presses a button. Each item has a position, starting at 0 for the top line.

Change Main.quorum to this program. It adds a button that puts Bread at the top of the list and takes Ham out.

use Libraries.Interface.Forms.Form
use Libraries.Interface.Forms.Page
use Libraries.Interface.Forms.FormBehavior
use Libraries.Interface.Events.BehaviorEvent
use Libraries.Interface.Controls.Button
use Libraries.Interface.Controls.List
use Libraries.Interface.Controls.ListItem

class Main is FormBehavior
    action Main
        Form form
        Page page = form:GetMainPage()
        page:SetTitle("Shopping")

        List shoppingList = page:AddList("Shopping list")
        shoppingList:SetDescription("Things to buy at the store")
        shoppingList:Add("Milk")
        shoppingList:Add("Eggs")
        shoppingList:Add("Ham")
        shoppingList:Add("Cheese")
        shoppingList:Select(0)

        ListBehavior behavior
        shoppingList:SetItemActivationBehavior(behavior)

        Button change = page:AddButton("Change List")
        change:SetDescription("Adds Bread to the top of the list and removes Ham")
        change:SetBehavior(me)

        form:Display()
    end

    action Run(BehaviorEvent event)
        Page page = GetPage()
        List shoppingList = page:GetList("Shopping list")

        shoppingList:Add(0, "Bread")
        ListItem removed = shoppingList:Remove("Ham")

        text first = shoppingList:Get(0)
        integer count = shoppingList:GetSize()
        output "The first item is " + first + ". There are " + count + " items."

        shoppingList:Select(0)
    end
end

Here is what the button's Run action does:

  • GetPage returns the page, and GetList finds the list by its name.
  • Add(0, "Bread") puts "Bread" at the top.
  • Remove("Ham") takes out the first item with that text. It returns the ListItem it removed, or undefined if there was no Ham.
  • Get(0) returns the text at the top, and GetSize returns how many items there are.
  • Select(0) moves the selection to the top item.

Press the button once. The output says "The first item is Bread. There are 4 items." What happens if you press it again?

Blocks

Adding a picture

An item can have a small picture, called an icon, next to its text. Pictures can only be made while the program is running, so make them in a behavior's Run action, like the one above. Make an Icon, load a shape or image into it, and pass it to Add with the text:

use Libraries.Interface.Controls.Icon
use Libraries.Game.Graphics.Color
Color color
Icon appleIcon
appleIcon:LoadFilledCircle(8, color:Green())
shoppingList:Add("Apples", appleIcon)

An icon is only a picture. A screen reader still reads the text, so make sure the text says everything a person needs to know.

One item, its own behavior

Every item uses the list's item activation behavior unless it has its own. To give one item a different behavior, get its ListItem and set its activation behavior. Here, SaleBehavior stands for another behavior class you write:

SaleBehavior saleBehavior
ListItem cheese = shoppingList:GetListItem(3)
cheese:SetActivationBehavior(saleBehavior)

Engage

In this activity, you build a list and test how easy it is to use with the keyboard. The goal is to create a list whose items do something useful when activated, and to find out how people move through a list without a mouse. Build a list of five songs, games or foods. When an item is activated, the program should write something about that item, not only its name, such as "Pizza: a favorite on Fridays."

Then test your list with only the keyboard, no mouse. Answer these questions in writing:

  • Could you reach every item with the arrow keys and the first letter keys?
  • What would you change to make the list easier to use?
  • First letter navigation jumps to the next item that starts with that letter. What does that tell you about lists with many items that have the same first letter?

Make that change, and test the list again.

Wrap up

Lists are one of the most common controls on any computer. Think about how your list stored its items and how a person moved through them. How is a List control like the arrays you have used before, and how is it different?

Next tutorial: Trees