Markus Rossler
Markus Rossler

Reputation: 163

flex 4 combobox selectedIndex failure

Having a ComboBox with a dataprovider having n>3 stringvalues in it.

combobox.inputText.text = "value in dataprovider";
// OK, selectedIndex is set also

several seconds later, initiated by user pressed button:

combobox.selectedIndex = 3; 
// OK

again some seconds later do this

combobox.inputText.text = "value NOT in dataprovider";

the last line sets combobox.inputText, but lets selectedIndex be 3 although the inputText-value is not in the dataprovider values.

This can be proven with the following example by pressing button 1, then button 4, then again button 1.

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" 
               initialize="initializeHandler(event)">
    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import mx.events.FlexEvent;
            [Bindable] private var array : ArrayCollection;

            protected function initializeHandler(event:FlexEvent):void {
                array = new ArrayCollection();
                array.addItem("0:00");
                array.addItem("0:30");
                array.addItem("1:00");
                array.addItem("1:30");
                addEventListener(Event.ENTER_FRAME, ef);
            }

            protected function btnSelect1_clickHandler(event:MouseEvent):void {
                cb.selectedIndex = 3;
            }

            protected function btnSelect2_clickHandler(event:MouseEvent):void {
                cb.selectedIndex = -1;
            }

            protected function btnSelect3_clickHandler(event:MouseEvent):void {
                cb.textInput.text = "1:00";
            }

            protected function btnSelect4_clickHandler(event:MouseEvent):void {
                cb.textInput.text = "1:01";
            }

            protected function ef(event:Event):void {
                l.text = "inputText=\"" + cb.textInput.text + "\" selectedIndex=\""+cb.selectedIndex+"\"";
            }

        ]]>
    </fx:Script>
    <s:VGroup>
        <s:ComboBox id="cb" dataProvider="{array}"/>
        <s:Button label="select index 3" click="btnSelect1_clickHandler(event)" /> 
        <s:Button label="select index -1" click="btnSelect2_clickHandler(event)" />
        <s:Button label="select '1:00'" click="btnSelect3_clickHandler(event)" /> 
        <s:Button label="select '1:01'" click="btnSelect4_clickHandler(event)" />
        <s:Label id="l" />
    </s:VGroup>
</s:Application>

Upvotes: 1

Views: 1999

Answers (1)

HugoLemos
HugoLemos

Reputation: 473

Try not manipulate directly the inputText. Instead, use the method getItemIndex to find the object in the ArrayCollection. See the functions rewrited above:

protected function btnSelect3_clickHandler(event:MouseEvent):void {
    cb.selectedIndex = array.getItemIndex("1:00");
}

protected function btnSelect4_clickHandler(event:MouseEvent):void {
    cb.selectedIndex = array.getItemIndex("1:01");
}

Upvotes: 1

Related Questions