Tuesday, March 8, 2011

Spark ComboBox and selectedIndex issues

FYI, there is a barely documented "feature" in the spark ComboBox. The property is allowCustomSelectedItem = true (which BTW is mx_internal namespace, so you can't change it unless you override the component or include the namespace)
What this will do, is allow the user to type in a value that is NOT in the dataprovider. If this happens, you get a selectedIndex of -3 and a valid selectedItem...

Here is your pitfall

if (comboBox.selectedIndex != -1)
{
myImportantValue = comboBox.selectedItem
}
You could end up with non-valid user inputed values here.... the better solution is.
if (comboBox.selectedIndex >=0)
{
myImportantValue = comboBox.selectedItem
}

No comments:

Post a Comment