5 Replies Latest reply on May 22, 2009 8:47 AM by cputnam14

    Combo box set selected text.

    cputnam14

      Hey folks,

       

      I have this wierd thing happening with a combo box. I am using a loop to populate an array. The array is the data provider for a combo box. Everything is working fine exept the first vaule of the array doesn't show up in the combobox as the selected value. I've tried setting the selected index to -1, 0, 1 but it never displays as soon as the page loads.

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="buildcombo();">
         
           <mx:Script>
              <![CDATA[
                  [Bindable] public var MyOptions:Array = new Array();
             
              private function buildcombo():void
              {
                 
                  MyOptions.push({
                  label:"-SELECT-", data:0
                         });
                 
                  for(var i:int=1;i<10;i++)
                  {
                  MyOptions.push({
                  label:"OPTION " + i, data:i.toString()
                         });
                  }
                 
                 
              }
             
             
              private function reset():void
              {
                  MyOptions = new Array();
                  buildcombo();
              }
             
              private function ShowValue():void
              {
                  trace(MyCombo.value);
              }
             
             
              ]]>
          </mx:Script>
          <mx:ComboBox id="MyCombo" x="325" y="145" dataProvider="{MyOptions}" change="ShowValue();"/>
          <mx:Button x="362" y="224" label="Reset" click="reset();"/>
      </mx:Application>

        • 1. Re: Combo box set selected text.
          BennyBeta Level 4

          Use the ComboBox's prompt property:


          <mx:ComboBox id="MyCombo" x="325" y="145" prompt="-SELECT-" dataProvider="{MyOptions}" change="ShowValue();"/>

           

          Let me know if that does the trick.

           

          Ben Edwards

          1 person found this helpful
          • 2. Re: Combo box set selected text.
            run,ryan! Level 3
            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="buildcombo();">
               
                 <mx:Script>
                    <![CDATA[
                        [Bindable] public var MyOptions:Array = new Array();
                   
                    private function buildcombo():void
                    {
                       var arr:Array = new Array();
                        arr.push({
                        label:"-SELECT-", data:0
                               });
                       
                        for(var i:int=1;i<10;i++)
                        {
                        arr.push({
                        label:"OPTION " + i, data:i.toString()
                               });
                        }
                       
                       MyOptions = arr;
                       
                    }
                   
                   
                    private function reset():void
                    {
                        MyOptions = new Array();
                        buildcombo();
                    }
                   
                    private function ShowValue():void
                    {
                        trace(MyCombo.selectedLabel);
                    }
                   
                   
                    ]]>
                </mx:Script>
                <mx:ComboBox id="MyCombo" x="325" y="145" dataProvider="{MyOptions}" updateComplete="ShowValue()"/>
                <mx:Button x="362" y="224" label="Reset" click="reset();"/>
            </mx:Application>
            • 3. Re: Combo box set selected text.
              cputnam14 Level 1

              Ben, That is a good thing to know, but it doesn't set the value on the select.

               

              Ryans solution works...I am just wondering why that makes a difference.

              • 4. Re: Combo box set selected text.
                run,ryan! Level 3

                1, "change" is event triggered by user interaction, not programmatically

                2, you don't have 'value' in your dp, only label

                3, as your dp is bindable, use a temp var when changing it, otherwise cost more resource

                • 5. Re: Combo box set selected text.
                  cputnam14 Level 1

                  I just used change for this example...the actual application is much more complex...when a user selects something from the drop down, a webservice gets fired and the result populates another drop down.

                   

                  i have data in my dataprovider...thats what gets traced back.

                   

                  I still don't understand why doing a push to the dp array causes the text to not show up by default, but doing a push to a temp variable and then setting the dp = to the temp variable does cause the text to show up.