ComboBox

Package: Libraries.Interface.Controls · Inherits from: Libraries.Interface.Events.FocusListener, Libraries.Interface.Events.TextChangeListener, Libraries.Interface.Controls.Control, Libraries.Interface.Events.MouseMovementListener, Libraries.Interface.Item, Libraries.Interface.Item2D, Libraries.Interface.Events.SelectionListener, Libraries.Language.Object, Libraries.Interface.Events.MouseListener, Libraries.Interface.Controls.TextField

The ComboBox class is a text field with a list of choices. A chevron at the field's right edge opens the list below the field, or above it when there is no room below. The user can pick a choice from the list or type in the field. Typing narrows the list to the choices that start with the typed text, ignoring case; when no choice matches, the list shows no choices. Moving through the list with the arrow keys writes each choice into the field, so the focus never leaves the field, and Enter chooses it. Escape closes the list, and a second Escape puts back the text the field had when the list opened. Programs add choices with Add, choose one with Select, and hear the user's choices with a ComboBoxListener. Its colors come from the interface options under the key "ComboBox": the field's background and border, and the foreground color for the chevron.

Example Code

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

Form form
Page page = form:GetMainPage()

ComboBox sizes
sizes:SetName("Size")
sizes:Add("Small")
sizes:Add("Medium")
sizes:Add("Large")
sizes:SetPlaceholder("Choose a size")
page:Add(sizes)

form:Display()

Actions

Add(integer index, text item)

This action adds a choice at the given place in the list. Index 0 is the top of the list, and the choices at or after it move down by one.

Parameters

  • index integer: The index the new choice will have.
  • item text: The choice's text.

Example

use Libraries.Interface.Controls.ComboBox

ComboBox sizes
sizes:Add("Large")
sizes:Add(0, "Small")

Add(text item)

This action adds a choice to the end of the list.

Parameters

  • item text: The choice's text.

Example

use Libraries.Interface.Controls.ComboBox

ComboBox sizes
sizes:Add("Small")
sizes:Add("Large")

AddComboBoxListener(Libraries.Interface.Events.ComboBoxListener listener)

This action adds a listener that is told each time the user chooses a different choice.

Parameters

Example

use Libraries.Interface.Controls.ComboBox
use Libraries.Interface.Events.ComboBoxListener
use Libraries.Interface.Events.ComboBoxChangedEvent

class Main is ComboBoxListener
    integer chosen = -1

    action Main
        ComboBox sizes
        sizes:Add("Small")
        sizes:AddComboBoxListener(me)
    end

    action ComboBoxChanged(ComboBoxChangedEvent event)
        chosen = event:GetNewIndex()
    end
end

Cancel()

This action does what Escape does in the combo box. With the list open, it closes the list. Pressed again right after, it puts back the text the field had when the list opened. It returns false when it did neither, so Escape can do what it does elsewhere, such as close a tooltip.

Returns

boolean: true if Escape was used by the combo box.

Example

use Libraries.Interface.Controls.ComboBox

ComboBox sizes
sizes:Add("Small")
sizes:Open()
boolean used = sizes:Cancel()

ChooseHighlighted()

This action chooses the highlighted choice as the user does: its text is put in the field, combo box listeners are told if the choice changed, and the list closes. With nothing highlighted, the field's text is kept, the choice whose text is the same is chosen (or none), and the list closes.

Example

use Libraries.Interface.Controls.ComboBox

ComboBox sizes
sizes:Add("Small")
sizes:Open()
sizes:MoveHighlight(1)
sizes:ChooseHighlighted()

ClickedMouse(Libraries.Interface.Events.MouseEvent event)

This action is called for a click on the field, and for a click outside the open list. A click on the field, the chevron included, opens the list, or closes it if it is open; a click anywhere but the list and the field closes it. The input system calls this, so most users won't need to call it directly.

Parameters

Close()

This action closes the list, if it is open. The field keeps its text.

Example

use Libraries.Interface.Controls.ComboBox

ComboBox sizes
sizes:Add("Small")
sizes:Open()
sizes:Close()

Empty()

This action empties the combo box: it removes every choice, chooses nothing and clears the field's text.

Example

use Libraries.Interface.Controls.ComboBox

ComboBox sizes
sizes:Add("Small")
sizes:Empty()

GainedFocus(Libraries.Interface.Events.FocusEvent event)

This action is called when the field gets the focus. It does what a text field does. (A text field gets this action from two parents, so a subclass has to name the one it means.)

Parameters

GetActiveDescendant()

This action returns the highlighted row of the open list, which is also the list's selection, or undefined when the list is closed or no row is highlighted. The focus stays in the field, but screen readers are pointed at this row, so they read the choice the arrow keys just moved to.

Returns

Libraries.Interface.Item: The highlighted row, or undefined.

Example

use Libraries.Interface.Controls.ComboBox
use Libraries.Interface.Item

ComboBox sizes
sizes:Add("Small")
sizes:Open()
sizes:MoveHighlight(1)
Item active = sizes:GetActiveDescendant()

GetChoice(integer index)

This action returns the text of the choice at the given index.

Parameters

  • index integer: The index of the choice.

Returns

text: The choice's text.

Example

use Libraries.Interface.Controls.ComboBox

ComboBox sizes
sizes:Add("Small")
text first = sizes:GetChoice(0)

GetChoiceCount()

This action returns how many choices the combo box has. The text field's own GetSize returns the number of characters in the field.

Returns

integer: The number of choices.

Example

use Libraries.Interface.Controls.ComboBox

ComboBox sizes
sizes:Add("Small")
integer count = sizes:GetChoiceCount()

GetChoiceList()

This action returns the list that opens below the field, which shows the choices matching the field's text. Accessibility tools use it to tie the field to its list; most programs don't need it.

Returns

Libraries.Interface.Controls.List: The combo box's list.

Example

use Libraries.Interface.Controls.ComboBox
use Libraries.Interface.Controls.List

ComboBox sizes
List shown = sizes:GetChoiceList()

GetHighlightedIndex()

This action returns the highlighted row of the list, counted among the choices the list shows (GetMatchingItems), or -1 when no row is highlighted.

Returns

integer: The highlighted row, or -1.

Example

use Libraries.Interface.Controls.ComboBox

ComboBox sizes
sizes:Add("Small")
sizes:Open()
sizes:MoveHighlight(1)
integer row = sizes:GetHighlightedIndex()

GetMatchingItems()

This action returns the choices the list shows: those that start with the text the user last typed, ignoring case, or every choice when the list was opened without typing. Moving the highlight writes a choice into the field without changing these.

Returns

Libraries.Containers.Array<text>: The texts of the choices in the list, in order.

Example

use Libraries.Interface.Controls.ComboBox
use Libraries.Containers.Array

ComboBox sizes
sizes:Add("Small")
sizes:Add("Large")
sizes:Open()
Array<text> shown = sizes:GetMatchingItems()

GetPlaceholder()

This action returns the words shown in the field while it is empty, or an empty text when there are none.

Returns

text: The placeholder's words.

Example

use Libraries.Interface.Controls.ComboBox

ComboBox sizes
sizes:SetPlaceholder("Choose a size")
text words = sizes:GetPlaceholder()

GetSelectedIndex()

This action returns the index of the chosen choice, or -1 when nothing is chosen.

Returns

integer: The index of the chosen choice, or -1.

Example

use Libraries.Interface.Controls.ComboBox

ComboBox sizes
sizes:Add("Small")
sizes:Select(0)
integer index = sizes:GetSelectedIndex()

GetSelectedItem()

This action returns the text of the chosen choice, or undefined when nothing is chosen.

Returns

text: The chosen choice's text, or undefined.

Example

use Libraries.Interface.Controls.ComboBox

ComboBox sizes
sizes:Add("Small")
sizes:Select(0)
text chosen = sizes:GetSelectedItem()

IsOpen()

This action returns whether the list is open on the screen.

Returns

boolean: true if the list is showing.

Example

use Libraries.Interface.Controls.ComboBox

ComboBox sizes
boolean open = sizes:IsOpen()

LoadGraphics(Libraries.Interface.Layouts.LayoutProperties properties)

This action loads the graphics for the combo box. It lets TextField load the field, then colors the chevron in the "ComboBox" foreground color and the placeholder in a lighter shade of the text color. The game engine calls this as needed, so most users won't need to call it directly.

Parameters

LostFocus(Libraries.Interface.Events.FocusEvent event)

This action is called when the field loses the focus. It closes the list, keeping the field's text, and chooses the choice whose text is the field's text, or none, telling combo box listeners if the choice changed.

Parameters

MoveHighlight(integer step)

This action moves the highlight in the list by the given number of rows, stopping at the first and last rows, as the arrow keys do. The field's text stays as it is until a choice is chosen (see ChooseHighlighted), so screen readers name the highlighted choice rather than a change to the text. With nothing highlighted, a step of 1 highlights the first row and a step of -1 the last. A step as large as the list goes to the first or last row.

Parameters

  • step integer: How many rows to move: positive moves down, negative moves up.

Example

use Libraries.Interface.Controls.ComboBox

ComboBox sizes
sizes:Add("Small")
sizes:Add("Large")
sizes:Open()
sizes:MoveHighlight(1)

MovedMouse(Libraries.Interface.Events.MouseEvent event)

This action is called when the mouse moves over the field, over the open list, or anywhere the list's layer has nothing. Over a row of the list, that row is tinted with the "ComboBoxChoice" mouse-over color; anywhere else, no row is. The row the keyboard highlights keeps the selection color. The input system calls this, so most users won't need to call it directly.

Parameters

Open()

This action opens the list below the field, or above it when there is no room below, showing every choice with the chosen one highlighted. If the list is open, nothing happens.

Example

use Libraries.Interface.Controls.ComboBox

ComboBox sizes
sizes:Add("Small")
sizes:Add("Large")
sizes:Open()

RemoveAt(integer index)

This action removes the choice at the given index. The choices after it move up by one. If it was the chosen one, nothing is chosen, and the field keeps its text.

Parameters

  • index integer: The index of the choice to remove.

Example

use Libraries.Interface.Controls.ComboBox

ComboBox sizes
sizes:Add("Small")
sizes:Add("Large")
sizes:RemoveAt(0)

RemoveComboBoxListener(Libraries.Interface.Events.ComboBoxListener listener)

This action removes a listener, so it is no longer told of the user's choices.

Parameters

Example

use Libraries.Interface.Controls.ComboBox
use Libraries.Interface.Events.ComboBoxListener
use Libraries.Interface.Events.ComboBoxChangedEvent

class Main is ComboBoxListener
    action Main
        ComboBox sizes
        sizes:AddComboBoxListener(me)
        sizes:RemoveComboBoxListener(me)
    end

    action ComboBoxChanged(ComboBoxChangedEvent event)
    end
end

Select(integer index)

This action chooses the choice at the given index, as a program does: its text is put in the field, and combo box listeners aren't told. With -1, nothing is chosen and the field is emptied, so the placeholder shows.

Parameters

  • index integer: The index of the choice, or -1 for none.

Example

use Libraries.Interface.Controls.ComboBox

ComboBox sizes
sizes:Add("Small")
sizes:Add("Large")
sizes:Select(1)

SelectionChanged(Libraries.Interface.Events.SelectionEvent event)

This action is called when the list's selection changes. When the user clicked a choice in the list, it chooses that choice and closes the list. The list calls this, so most users won't need to call it directly.

Parameters

SetPlaceholder(text value)

This action sets the words shown in the field while it is empty, such as "Choose a size". An empty text shows nothing.

Parameters

  • value text: The placeholder's words.

Example

use Libraries.Interface.Controls.ComboBox

ComboBox sizes
sizes:SetPlaceholder("Choose a size")

TextChanged(Libraries.Interface.Events.TextChangeEvent event)

This action is called when the field's text changes. It shows or hides the placeholder. When the user changed the text while the field has the focus, it narrows the list to the choices that start with the text and opens it. Text the combo box writes itself, such as a highlighted choice, doesn't narrow the list. The text field calls this, so most users won't need to call it directly.

Parameters

Inherited Actions

258 actions from Libraries.Interface.Controls.Control
71 actions from Libraries.Interface.Controls.TextField
59 actions from Libraries.Interface.Item
201 actions from Libraries.Interface.Item2D
3 actions from Libraries.Language.Object