Flex: programmatically setting a selected item in a ComboBox

I need help that programmatically sets the selected item to a combo box.

I have a combobox:

<mx:ComboBox id="MyComboBox" change="puzzleHandler(event);" prompt="Make a Selection"> <mx:ArrayCollection id="myDP"> <mx:Object id="first" label="Label 1" series="2" pageTitle="Title 1"/> <mx:Object id="second" label="Label 2" series="7" pageTitle="Title 2"/> <mx:Object id="third" label="Label 3" series="9" pageTitle="Title 3"/> </mx:ArrayCollection> </mx:ComboBox> 

I have a function that deals with deep binding. If someone places the URL: www.mysite.com/#view=2, they will be sent to the corresponding part of the site (without choosing Label 2 in comboBox). How to configure comboBox programmatically to match what it is looking at?

In my switch switch statement, I want to set comboBox to a label corresponding to the view. If "view = 2", then the comboBox should display "Label 2" as selected.

  case "view=1": MyComboBox.selectedItem.label="Label 1"; parseUrl(); case "view=2": MyComboBox.selectedItem.label="Label 2"; parseUrl(); case "view=3": MyComboBox.selectedItem.label="Label 3"; parseUrl(); 

I tried this: MyComboBox.selectedItem.label = "Label 1" But it does not work. Any suggestions?

Thanks.

-Laxmidi

+6
flex combobox
source share
1 answer

You do not want to modify the selectedItem object; You want to change the selected item or SelectedIndex. Try the following:

 case "view=1": MyComboBox.selectedIndex=0; parseUrl(); case "view=2": MyComboBox.selectedIndex=1; parseUrl(); case "view=3": MyComboBox.selectedIndex=2; parseUrl(); 

IF you want to set selectedItem instead of the selected Index, you will have to iterate over the dataProvider to find the actual item based on the case / URL value. Something like that:

 for each(var tempObject : Object in myList.dataProvider){ if(tempObject.label == urlValue){ MyComboBox.selectedItem = tempObject; break; } } 

The second approach is more flexible in the long run.

+8
source share

All Articles