Search code examples
actionscript-3apache-flexcomboboxflex3

How to get value of selected index in combo Box when user select the value from it in flex


Hi I have created a comboBox dynamically and Since I'm new to Flex I have no idea how to get the selected value from combo box when user select a value from combobox dropdown

Below is my code

var comboBox:ComboBox = new ComboBox();
comboBox.dataProvider = field.getValues();
comboBox.width = "50";
comboBox.prompt = "Test";
comboBox.selectedIndex = -1;

Could someone help me out in order to identify how I'll be able to get value of a selected index when user will select the value from dropdown of combo box ?

Even a sample example will help me !!

Thanks in Advance.....!!


Solution

  • You can do like following way:

    var comboBox:ComboBox = new ComboBox();
    comboBox.dataProvider = field.getValues();
    comboBox.width = 50;
    comboBox.prompt = "Test";
    comboBox.selectedIndex = -1;
    comboBox.addEventListener(ListEvent.CHANGE, onChange);
    panel.addChild(comboBox);
    
    private function onChange(event:Event):void
    {
        trace(event.currentTarget.selectedItem); //Here you get the selected item.
    }
    

    Hope it helps.