6 Replies Latest reply on Jan 4, 2007 11:42 AM by jeseiter

    ComboBox - trying to set the selected item

    FranceC Level 1
      I am calling a remote object that calls the following function as the result. The returned object is used to populate a form of TextInputs and a ComboBox -- it's an Edit form. How can I set the selected item in the ComboBox? I have been unable to get selectedItem to work, and I don't know the value of the index that corresponds to selectedCompany.CompanyType to be able to use selectedIndex.

      private function receiveSelectedCompany(event:ResultEvent):void {
      selectedCompany = event.result as Company;

      //copy the record to the Edit fields
      txtCompanyId.text = selectedCompany.CompanyID.toString();
      //cboCompanyTypeEdit.selectedItem.CompanyType = selectedCompany.CompanyType;
      txtCompanyName.text = selectedCompany.CompanyName;
      txtCompanyShortName.text = selectedCompany.CompanyShortName;
      txtTaxId.text = selectedCompany.TaxID;

      Thanks in advance,
        • 1. Re: ComboBox - trying to set the selected item
          inlineblue Level 1
          The way I've been doing it is to simply loop through the combobox's dataprovider, find the matching item, and then set the selectedIndex.
          • 2. Re: ComboBox - trying to set the selected item
            FranceC Level 1
            Thank you for the response! I had thought about doing that, but was hoping there may be an easier way. If that is the best way, do you have a suggestion for how to access the ArrayCollection that is populating the ComboBox as I have the code for the ComboBox in a custom component by itself? Is it better to perform the loop in the main component [if so, how do I access the ArrayCollection that is inside the ComboBox component], or in the ComboBox component [in which I would have to pass to the ComboBox the returned value that I want selected, and set the selectedIndex there]?

            Thanks again,
            • 3. Re: ComboBox - trying to set the selected item
              FranceC, heres a function you can use to do that.

              simply call the selectComboValue function and pass it the combobox name and the string to look for like so :

              selectComboValue(myComboBox, dataObj.fieldName.toUpperCase());

              heres the function :

              public function selectComboValue(select: mx.controls.ComboBox, optionToSelect:String):void{
              for (var i:int = 0; i < select.dataProvider.length ; i++) {
              if (select.dataProvider.getItemAt( ).label == optionToSelect){
              select.selectedIndex = i;

              PS : if you are looking for the data rather than the label simply change this line :
              if (select.dataProvider.getItemAt(
              ).label ==
              for this :
              if (select.dataProvider.getItemAt( ).data ==
              • 4. Re: ComboBox - trying to set the selected item
                FranceC Level 1
                Thank you Diemos! I've got it working as you've suggested!

                • 5. Re: ComboBox - trying to set the selected item
                  Diemos Level 1
                  you're welcome :)

                  PS : could you mark the reply as the answer to your question ? Thanks!
                  • 6. ComboBox - trying to set the selected item

                    Great solution! It seems to work fine when I invoke the selectComboValue fxn for a Combox within a VBox.

                    When that same VBox is nested within an Accordian .. Repeater .. HBox construct, I get the following error msg:

                    1067: Implicit coercion of a value of type Array to an unrelated type mx.controls:ComboBox.

                    Any thoughts?

                    Thanks again,