Saurabh
Saurabh

Reputation: 985

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.....!!

Upvotes: 0

Views: 1787

Answers (2)

ketan
ketan

Reputation: 19341

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.

Upvotes: 0

Clintm
Clintm

Reputation: 4887

You can use comboBox.selectedItem.

Remember to check for null as selectedItem will return null if it is not set.

comboBox.addEventListener(ListEvent.CHANGE, comboBox_change, false, 0, true); //weak listener

private function comboBox_change(event:Event):void {
  var comboBox:ComboBox = event.target as ComboBox
  var item:MyClass = comboBox.selectedItem as MyClass
  if(item) {
    //do what you need to do
  }
}

Upvotes: 1

Related Questions