xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
import spark.events.IndexChangeEvent;
// Define a custom function for the labelFunction property
// to display an Object in the ComboBox control.
public function myLabelFunc(item:Object):String {
return item.firstName + " " + item.lastName;
}
// Define a custom function for the labelToItemFunction property
// to convert the new value to an Object of the correct format
// for storage in the data provider of the control.
public function myLabelToItemFunc(value:String):Object {
var tempObj:Object = new Object();
var spaceChar:int = value.indexOf(' ');
tempObj.firstName = value.substr(0, spaceChar);
tempObj.lastName = value.substr(spaceChar+1, value.length);
return tempObj;
}
// Event handler to determine if the selected item is new.
protected function myCB_changeHandler(event:IndexChangeEvent):void
{
// Determine if the index specifies a new data item.
if(myCB.selectedIndex == spark.components.ComboBox.CUSTOM_SELECTED_ITEM)
// Add the new item to the data provider.
myCB.dataProvider.addItem(myCB.selectedItem);
}
labelToItemFunction="myLabelToItemFunc"
change="myCB_changeHandler(event);">