11.20. Prepopulating a Form
Problem
You want to programmatically fill in a form with defaults or a user’s previous selections.
Solution
Use the methods and properties that allow you to set the values of each form element.
Discussion
Each type of form element has different ways of selecting the element
or modifying its value. For example, you can set a text
field’s contents using the text
property:
myTextField_txt.text = value;
You can set combo boxes and single-select list boxes using the
setSelectedIndex( )
method. This method requires
you to know the index of the item you wish to select programmatically
(and not just its label or data value).
// Selects the third item from the menu myComboBox_cb.setSelectedIndex(2);
You can programmatically select multiple items from a
multiple-selection list box using the setSelectedIndices(
)
method,
which accepts an array of indexes to select:
// Selects the first three menu items from a multiple-selection list box myListBox_lb.setSelectedIndices([0, 1, 2]);
To set a combo box or list box by its label
or
data
property (rather than by its index), you can
create a custom setSelectedItem( )
method for
FSelectableListClass
(the superclass of both
combo boxes and list boxes). The method should accept a single
parameter, which is the data value for the desired menu item. If the
data
properties of the menu items are all
undefined, the setSelectedItem( )
method should
try to match the value to a label instead. Additionally, you can
create a setSelectedItems( ...
Get Actionscript Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.