Sliders

Overview

A slider lets a person choose a number by moving a small knob, called the thumb, along a line, called the track. You use sliders to set the volume on a phone, the brightness of a screen, or the size of a brush in a drawing program. In this tutorial, you will add sliders to a form, set the smallest and largest numbers they allow and how far one key press moves them, read the number a person chose, and make your program react when the number changes.

Goals

You have the following goals for this tutorial:

By the end of this lesson, you can

  • Add a slider to a form with AddSlider, and a slider with a number box with AddSliderField
  • Set a slider's minimum, maximum and step, and explain how the step limits which numbers can be chosen
  • Read a slider's number with GetValue and GetNumberText
  • Use a SliderListener to react when a person moves a slider
  • Explain how a person using a keyboard or a screen reader uses a slider

Warm up

Think about the volume on a phone or a computer. How many different volume levels do you think it has? Would it be better with fewer levels, or with more? When does it matter to choose an exact number, and when is "about right" good enough?

Vocabulary

You will be learning about the following vocabulary words:

Vocabulary
TermDefinition
SliderA control for choosing a number by moving a thumb along a track.
ThumbThe small circle a person drags along a slider's track. Its place on the track shows the number.
MinimumThe smallest number a slider can choose. The left end of the track.
MaximumThe largest number a slider can choose. The right end of the track.
StepHow much one press of an arrow key changes a slider's number. Dragging the thumb also moves in steps.
Page stepHow much one press of Page Up or Page Down changes a slider's number.
Slider fieldA slider with a small number box beside it. The box shows the number, and a person can type an exact number in it.
GranularityHow finely something can be measured or chosen. A slider with a step of 0.1 has finer granularity than one with a step of 1.

New Code to Learn

You will be using the following new pieces of code:

New Code to Learn
Quorum CodeCode ExampleExplanation
AddSlider(text name, number minimum, number maximum, number step)Slider zoom = page:AddSlider("Zoom", 0, 4, 0.25)Adds a slider to a form page. The name is what a screen reader says. AddSlider(name) makes a slider from 0 to 100.
AddSliderField(text name, number minimum, number maximum, number step)SliderField price = page:AddSliderField("Price", 0, 50, 0.5)Adds a slider with a number box beside it.
GetValue()number value = zoom:GetValue()Returns the slider's number.
SetValue(number value)zoom:SetValue(1)Sets the slider's number from code. A number outside the range is moved to the nearest end.
GetNumberText()text shown = zoom:GetNumberText()Returns the number as text, with as many decimal places as the step needs, such as "0.25" or "40".
SetUnits(text units)price:SetUnits("dollars")Sets a word a screen reader says after the number.
GetSlider(text name)Slider zoom = page:GetSlider("Zoom")Finds a slider on a page by its name. GetSliderField finds a slider field.
AddSliderListener(SliderListener listener)zoom:AddSliderListener(listener)Tells the listener each time a person changes the number.
SliderChanged(SliderChangedEvent event)action SliderChanged(SliderChangedEvent event)The action a listener writes. The event's GetNewValue, GetOldValue and IsFinal say what changed.

CSTA Standards

This tutorial covers the following CSTA standards:

For teachers · 2026 CSTA K–12 Computer Science Standards

MS-DAT-DC-21

Middle School · Data & Analysis

Evaluate how different levels of precision and granularity in data collection affect accuracy, storage, and analysis.

Also in 3 other lessons

HS-DAT-DC-24

High School · Data & Analysis

Evaluate different approaches to verifying consistency and compliance with expected data types, values, and ranges.

Also in 3 other lessons

HS-ALG-IM-09

High School · Algorithms & Design

Design a computing technology using human-centered design principles.

Also in 7 other lessons

Every standard, and the lessons that cover it

Explore

What is a slider?

In the Combo Boxes tutorial, a person picked a word from a list. A slider is for picking a number from a range: every number between a minimum and a maximum. The thumb's place on the track shows the number. At the left end, the slider is at its minimum. At the right end, it is at its maximum. The part of the track up to the thumb is filled in with color, so it shows how far along it is.

A slider can never go outside its range. If a program sets a number that is too big, the slider uses the maximum instead, and if it is too small, the slider uses the minimum. That makes a slider a good way to collect a number that must stay within limits, such as a volume from 0 to 100.

Making sliders on a form

Forms have two actions for sliders:

  • AddSlider adds a plain slider. It shows no number, so it is good for a setting where the feel matters more than the exact number, such as brightness.
  • AddSliderField adds a slider field: a slider with a small number box beside it. The box shows the number, and a person can type an exact number in it. It is good when the number matters, such as a price.

A label added with AddLabel just before either one is shown beside it.

The program below makes a settings form with one of each.

use Libraries.Interface.Forms.Form
use Libraries.Interface.Forms.Page
use Libraries.Interface.Controls.Slider
use Libraries.Interface.Controls.SliderField
use Libraries.Interface.Controls.Button

Form form
Page page = form:GetMainPage()
page:SetTitle("Room Settings")

page:AddLabel("Brightness")
Slider brightness = page:AddSlider("Brightness")
brightness:SetDescription("How bright the lights are, from 0 to 100.")
brightness:SetValue(60)

page:AddLabel("Temperature")
SliderField temperature = page:AddSliderField("Temperature", 60, 80, 0.5)
temperature:SetUnits("degrees")
temperature:GetSlider():SetDescription("The room temperature, from 60 to 80 degrees.")
temperature:GetField():SetDescription("The room temperature, from 60 to 80 degrees.")
temperature:SetValue(70)

Button save = page:AddButton("Save")
save:SetDescription("Shows the settings you chose.")

SaveBehavior behavior
save:SetBehavior(behavior)

form:Display()

AddSlider("Brightness") makes a slider from 0 to 100. The temperature slider field goes from 60 to 80, and each key press moves it by 0.5.

A slider field is really three parts: a label, a slider and a number box. Its GetSlider and GetField actions return the slider and the number box. A screen reader reads the slider and the box separately, so the sample gives each one the same description.

Moving a slider

The form sets the focus order for you. A plain slider is one Tab stop. A slider field is two: first the slider, then the number box.

With the focus on a slider, these keys change its number:

A slider's keys
KeyWhat it does
Right or UpMoves up one step
Left or DownMoves down one step
Shift with an arrowMoves a tenth of a step
Page Up, Page DownMoves up or down one page step
HomeMoves to the minimum
EndMoves to the maximum

With a mouse, you can drag the thumb, or click anywhere on the track to jump the thumb there. On a touch screen, you can drag the thumb with a finger.

In a slider field's number box, you can type an exact number. The slider moves to it when the focus leaves the box, for example when you press Tab. A number outside the range is moved to the nearest end, and text that isn't a number is put back the way it was.

Minimum, maximum and step

The step is how much one press of an arrow key changes the number. Dragging the thumb and clicking the track also move in steps. The page step is how much Page Up and Page Down change it.

You can give the minimum, the maximum and the step when you add the slider:

Slider zoom = page:AddSlider("Zoom", 0, 4, 0.25)

This slider goes from 0 to 4, and each key press moves it by 0.25. Its page step is ten steps, 2.5.

If you leave out the step, the form picks one for you:

Steps the form picks
You writeRangeStepPage step
AddSlider("Volume")0 to 100110
AddSlider("Speed", 1, 10)1 to 100.090.9
AddSlider("Opacity", 0, 1)0 to 10.010.1
AddSlider("Score", 0, 50)0 to 5015

The rule is: a step of 1 when the range is 10 or more, or a hundredth of the range when it is less. The page step is a tenth of the range. (The AddSliderField versions work the same way.)

You can also change these after the slider is made, with SetMinimum, SetMaximum, SetRange(minimum, maximum), SetStep and SetPageStep.

Precision and granularity

The step decides which numbers a person can choose. A slider from 0 to 10 with a step of 1 has only 11 choices: 0, 1, 2 and so on, up to 10. With a step of 0.5, it has 21 choices. With a step of 0.01, it has 1,001. How finely a person can choose is called granularity.

Finer isn't always better. Imagine a survey that asks students how many hours they slept last night:

  • with a step of 1, every answer is a whole number of hours. "7" might mean anything from about 6 and a half to 7 and a half hours;
  • with a step of 0.25, a student can answer "7.25," to the nearest quarter hour;
  • with a step of 0.01, a student could answer "7.38," but almost nobody knows their sleep to the minute. The answer looks exact, but it isn't more accurate, and it is harder for a person to set with the keys.

The step also changes how the number looks. GetNumberText returns the number with as many decimal places as the step needs: a step of 0.25 gives two places, such as "7.25," and a step of 1 gives none, such as "7." A screen reader reads the number the same way. If you want a different number of places, use SetDecimalPlaces.

When you choose a step, ask: how exact can a person's answer really be, and how exact does my program need it to be?

Reading the number

A slider's GetValue action returns its number. GetNumberText returns the same number as text, written the way the slider shows it. The "Save" button uses both. Make a new class in a file named SaveBehavior.quorum. It comes from FormBehavior, which gives it the GetPage action, so it can find the slider with GetSlider and the slider field with GetSliderField.

use Libraries.Interface.Forms.FormBehavior
use Libraries.Interface.Forms.Page
use Libraries.Interface.Events.BehaviorEvent
use Libraries.Interface.Controls.Slider
use Libraries.Interface.Controls.SliderField

class SaveBehavior is FormBehavior
    action Run(BehaviorEvent event)
        Page page = GetPage()

        Slider brightness = page:GetSlider("Brightness")
        if brightness not= undefined
            output "Brightness: " + brightness:GetNumberText()
            if brightness:GetValue() < 20
                output "That is quite dark."
            end
        end

        SliderField temperature = page:GetSliderField("Temperature")
        if temperature not= undefined
            Slider temperatureSlider = temperature:GetSlider()
            output "Temperature: " + temperatureSlider:GetNumberText() + " degrees"
        end
    end
end

Run the program, move both sliders and press "Save." The output shows the two numbers.

GetSlider doesn't look inside a slider field, so it can't find "Temperature." Use GetSliderField for slider fields.

Blocks

Reacting when the number changes

A slider tells its listeners each time a person changes its number. A listener is a class that comes from SliderListener and writes the SliderChanged action. The action gets a SliderChangedEvent, which has:

  • GetNewValue, the number after the change;
  • GetOldValue, the number before the change;
  • IsFinal, which is false while the person is still dragging the thumb, and true once the change is done;
  • GetSlider, the slider that changed.

While a person drags the thumb, the slider tells its listeners many times. If your program does something slow with the number, such as saving it to a file, check IsFinal and do it only once the drag ends.

Make a class in a file named VolumeListener.quorum. It writes the volume into a label on the page, and says in the output when a change is finished.

use Libraries.Interface.Events.SliderListener
use Libraries.Interface.Events.SliderChangedEvent
use Libraries.Interface.Controls.Slider
use Libraries.Game.Graphics.Label

class VolumeListener is SliderListener
    Label summary = undefined

    action SetLabel(Label newLabel)
        summary = newLabel
    end

    action SliderChanged(SliderChangedEvent event)
        Slider slider = event:GetSlider()
        if summary not= undefined
            summary:SetText("Volume: " + slider:GetNumberText())
        end
        if event:IsFinal()
            output "The volume is set to " + slider:GetNumberText()
        end
    end
end

Now give the listener to a slider:

use Libraries.Interface.Forms.Form
use Libraries.Interface.Forms.Page
use Libraries.Interface.Controls.Slider
use Libraries.Game.Graphics.Label

Form form
Page page = form:GetMainPage()
page:SetTitle("Music Player")

page:AddLabel("Volume")
Slider volume = page:AddSlider("Volume")
volume:SetDescription("How loud the music plays.")
volume:SetValue(50)

Label summary = page:AddLabel("Summary", "Volume: 50")

VolumeListener listener
listener:SetLabel(summary)
volume:AddSliderListener(listener)

form:Display()

Drag the thumb slowly. The label changes the whole time, but the output has one line, when you let go. Then press Right a few times: each key press is a finished change, so each one adds a line.

A program's own SetValue doesn't tell listeners, because a person didn't make the change. That is why the label starts at "Volume: 50" by hand.

What a screen reader says

When a slider gets the focus, a screen reader says its name, its number and that it is a slider, and reads the description. If you set units with SetUnits, it says them after the number, such as "70 degrees." When the person presses an arrow key, the screen reader reads the new number. The exact words depend on the screen reader. Screen readers also have their own ways to change a slider; for example, VoiceOver on the Mac uses VO+Up and VO+Down.

For a slider field, a screen reader user can use the slider or type in the number box. Both parts have the same name, so it is clear they belong together.

Colors

When a slider has the focus, its thumb grows, so the focus is easy to spot. A slider 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 key "Slider."

Engage

In this activity, you build a short sleep survey form. The goal is to choose the range and step of each slider on purpose. The form needs at least three sliders or slider fields, for example hours of sleep last night, how rested you feel from 1 to 10, and minutes of screen time before bed. Give each a label, a clear name, a description and units where they help. Add a "Submit" button that outputs every answer with GetNumberText.

Before you write any code, decide each slider's minimum, maximum and step, and write one sentence about each step: why is it not finer, and why is it not coarser?

When it works, fill in your survey three times as three made-up people: one who slept 4 hours, one who slept 9 and a half, and one who had no screen time at all. Use only the keyboard: Tab, the arrow keys, Page Up, Page Down, Home and End. For each person, check:

  • Can every answer be given exactly, or does the step force a different number?
  • How many key presses did the longest answer take? Would a different page step help?
  • Does the output match what you chose?
  • If you have a screen reader, turn it on: does it report each slider's name, number and units?

Pick one slider whose range or step you would change after this test, change it, and write a sentence explaining why.

Wrap up

A slider chooses a number between a minimum and a maximum, moving by a step. You can read the number with GetValue or GetNumberText, or react right away with a SliderListener. The step decides how finely a person can answer, and a finer step isn't always a more accurate one. Think back to the warm up: for the sliders you use every day, is the step about right? In the next tutorial, Progress Bars, you will show a number that the program changes instead of the person.