User Guide

Using the ComboBox component 161
5. Select Frame 1 in the Timeline, open the Actions panel, and enter the following code:
function change(evt){
trace(evt.target.selectedItem.label);
}
comboBox.addEventListener("change", this);
The last line of code adds a change event handler to the ComboBox instance. For more
information, see
ComboBox.change.
To create a ComboBox component using ActionScript:
1. Drag the ComboBox component from the Components panel to the current document’s
library.
This adds the component to the library, but doesnt make it visible in the application.
2. Select the first frame in the main Timeline, open the Actions panel, and enter the
following code:
this.createClassObject(mx.controls.ComboBox, "my_cb", 10);
my_cb.addItem({data:1, label:"One"});
my_cb.addItem({data:2, label:"Two"});
This script uses the method “UIObject.createClassObject()” on page 1362 to create the
ComboBox instance, and then uses “ComboBox.addItem()” on page 171 to add list items
to the ComboBox.
3. Now add an event listener and event handler function to respond when a ComboBox item
is selected:
// Create listener object.
var cbListener:Object = new Object();
// Create event handler function.
cbListener.change = function (evt_obj:Object) {
trace("Currently selected item is: " +
evt_obj.target.selectedItem.label);
}
// Add event listener.
my_cb.addEventListener("change", cbListener);
4.
Select Control >Test Movie, and click an item in the combo box to see a message in the
Output panel.