5 Replies Latest reply on Jul 30, 2006 6:33 PM by ddanone

    Count property for elements with collections?

    leotemp Level 1
      Pretty straight forward, im sure this is in the docs somewhere but searching for count is not very productive..

      Is there a method for returning the number of elements in a say combobox? like:
      var c:int = int(comboboxID.countmethod.toString());
        • 1. Re: Count property for elements with collections?
          ddanone Level 1
          Try this:

          var LB:ListBase=yourComboBox.dropdown;
          var number_of_elements:Number = LB.numChildren;
          • 2. Re: Count property for elements with collections?
            leotemp Level 1
            great, thank you! how about this one though.. how do you select a row in a combobox if all you have is the text value of the labels.. like a combobox with three rows with the labels "chicken","beef","wookie" is there a way of selecting say "beef" without iterating through the collection and trying to match the strings manually and then selecting the matching index?

            Im pretty sure saying
            myComboBox.selectedLabel="beef"; would just produce an error although
            myComboBox.selectedIndex=1 would work, which seems wierd
            • 3. Re: Count property for elements with collections?
              leotemp Level 1
              hrmmm.. when i use the snippet you provided i get the following warning:
              "Type was not found or was not a compile-time constant: ListBase."

              do i need to import something?
              • 4. Re: Count property for elements with collections?
                ddanone Level 1
                You must include:
                import mx.controls.listClasses.ListBase;
                • 5. Re: Count property for elements with collections?
                  ddanone Level 1
                  I'm sure there are a more automatic mode, here you have an example:

                  <?xml version="1.0"?>
                  <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml">

                  <mx:Script>
                  <![CDATA[

                  import mx.controls.Alert;
                  import mx.controls.listClasses.ListBase;

                  [Bindable]
                  public var cards: Array = [ {label:"Visa", data:1},
                  {label:"MasterCard", data:2},
                  {label:"American Express", data:3} ];

                  [Bindable]
                  public var selectedItem:Object;
                  public function setValue(value_to_establish:String):void{
                  var LB:ListBase=cb.dropdown;
                  var number_of_elements:Number = LB.numChildren;
                  for ( var x:Number=0; x<number_of_elements; x++ ) {
                  if ( value_to_establish == cb.dataProvider[x]['label'] ){
                  cb.selectedIndex = x;
                  break;
                  }
                  }

                  Alert.show('actual label: '+cb.selectedItem.label + "\nactual data: "+cb.selectedItem.data);

                  }
                  ]]>
                  </mx:Script>

                  <mx:Panel title="ComboBox Control Example"
                  height="75%" width="75%" layout="horizontal"
                  paddingTop="10" paddingBottom="10" paddingLeft="10" paddingRight="10">

                  <mx:ComboBox id='cb' dataProvider="{cards}" width="150"
                  close="selectedItem=ComboBox(event.target).selectedItem"/>

                  <mx:VBox width="250">
                  <mx:Text width="200" color="blue" text="Select a type of credit card."/>
                  <mx:Label text="You selected: {selectedItem.label}"/>
                  <mx:Label text="Data: {selectedItem.data}"/>
                  </mx:VBox>

                  <mx:Button label="change ComboBox value" click="setValue('MasterCard')"/>

                  </mx:Panel>
                  </mx:Application>



                  quote:

                  Originally posted by: leotemp
                  great, thank you! how about this one though.. how do you select a row in a combobox if all you have is the text value of the labels.. like a combobox with three rows with the labels "chicken","beef","wookie" is there a way of selecting say "beef" without iterating through the collection and trying to match the strings manually and then selecting the matching index?

                  Im pretty sure saying
                  myComboBox.selectedLabel="beef"; would just produce an error although
                  myComboBox.selectedIndex=1 would work, which seems wierd