Combobox set selectedItem

Combobox set selectedItem

Post by Solerou » Thu, 29 May 2008 01:24:05


I've seen a half dozen solutions to this on the internet but somehow they don't
work. Basically I just want to set the selectedItem of a combobox:

<mx:Combobox id="comboboxID" dataprovider="{httpserv.lastresult.system.data}"/>

// actionscript which won't work
comboboxID.selectedItem = "something";

So then I tried to loop through the comboboxID.dataprovider to find the index
for "something" but it won't ever give me the dataprovider strings, instead
giving me errors like this:

warning: unable to bind to property 'name' on class 'String' (class is not an
IEventDispatcher)

How can I get this working?

Thanks.
 
 
 

Combobox set selectedItem

Post by Solerou » Fri, 30 May 2008 04:24:16

Thanks for the interest. I think the problem was somehow in the way I was
defining my own custom combo box. I finally gave up and just wrote a loop to
get the matching index and set it manually. May not be the best solution, but
it works...



combo.selectedIndex = setComboBox(combo, "label_name");

private function setComboBox(box:ComboBox, val:String):int {

for ( var i:int=0; i<box.dataProvider.length; i++ ) {

if (box.dataProvider[i].name == val) { //compare desired value to
current item.data value
box.selectedIndex = i; //set the seletedIndex of the combo box
trace("found index: ",i);
return i;
}
}
return 0;
}