Flex ComboBox Fix
Ok, this one's been driving me nuts, so I finally fixed it myself.
The problem is with the infamous Flex ComboBox control. When you change data providers for the control at runtime, you lose the previously selected index.
Really.
Finding no joy for a solution online, I started tinkering with the SDK source code to see what was up:
http://tools.assembla.com/flexsdk/browser/mx/controls/ComboBox.as
I noticed a "set dataProvider" function on line 669 which was the culprit. It destroys the dropdown and invalidates any of the previous properties of the control. I'm guessing this is what's wiping out the selected index!
So I extended the ComboBox and added a property which stores the selected index in a separate variable. After calling:
super.dataProvider = dp;
...I restore the selected index to its saved value.
I set the value when the object is first rendered and whenever there's a change event on the ComboBox.
See the attached code ('download' link below) for a sample and the shiny new ComboBox2.
import mx.controls.ComboBox;
import mx.events.ListEvent;
import flash.events.Event;
public class ComboBox2 extends ComboBox {
private var savedSelectedIndex:int;
public var selectionChanged:Boolean = false;
public function ComboBox2( ) {
super();
this.savedSelectedIndex = this.selectedIndex;
this.addEventListener( ListEvent.CHANGE, onChange );
this.addEventListener( Event.RENDER, onRender );
}
// On first render of the ComboBox, capture the selected index
private function onRender( event:Event ):void {
savedSelectedIndex = this.selectedIndex;
this.removeEventListener( Event.RENDER, onRender );
}
// On change of selection on the ComboBox, capture the selected index
private function onChange( event:ListEvent ):void {
savedSelectedIndex = this.selectedIndex;
}
override public function set dataProvider( dp:Object ):void {
super.dataProvider = dp;
// Yipee! Restore the selectedIndex:
this.selectedIndex = savedSelectedIndex;
}
}
}
I could not have done this before the open source release of the Flex SDK to the world. Thanks Adobe! (But opinion withheld on the ComboBox;)

Nice work around though. For potential reuse, you could add a rememberSelectedIndex:Boolean property to the control to handle this.
@Chad: I ran into it on a project where we had to switch languages at runtime, and the previous selection would be lost. In the download zip there is an example of the selectedIndex being re-set to 0 after the dataProvider changes.
Listen, i´m extending ComboBox too. Whatever i do i don´t preserve the IFactory interface that it implements originally. That means i can´t do something like:
<components:Combo2>
<mx:itemRenderer>
<mx:Component>
<mx:Image......./>
</mx:Component>
</mx:itemRenderer>
</components:Combo2>
Does anybody have a clue?
Thanks