views:

329

answers:

1

I am facing a strange problem with the combobox in Flex. In the following code :

 public function rollCombo(cmb:ComboBox,value:String):void
 {
      if(value=='') return;
      var i:int=0;
      cmb.selectedIndex = 0;
      var dp1:XMLListCollection =   (XMLListCollection(cmb.dataProvider);
      trace(value);
      while(dp1[i]!=value && i<dp1.length)
        cmb.selectedIndex = ++i;
        cmb.validateNow();
        cmb.validateDisplayList();

   trace(cmb.selectedLabel);
 }

in an example case, at the end of the execution of the function, i is 7, and cmb.selectedLabel is "xyz"(according to the trace output), but the label displayed in the combobox is a different one.

Also, this is rather unpredictable. It happens sometimes and not always.

+2  A: 

The last selectedIndex is out of range, because you use pre-incrementation. Which means i becomes dp1.length before the test, and it's assigned to selectedIndex too. That might explain the weird behavior. You'll probably want to use post-incrementation.

Also. The only thing that gets executed in that while looks to be

cmb.selectedIndex = ++i;

I don't know if that's what you wanted, but you might need some "{}" there.

bug-a-lot