8 Replies Latest reply on May 18, 2009 7:42 AM by Mohd Fayaz

    ComboBox Manually setting to a particular Item

    Mohd Fayaz Level 1

      I have a ComboBox having the following Labels;

       

      Label1

      Label3

      Label2

      Label4

       

      How to change the Label displayed in ComboBox from code. For example, Lets assume I have four button (Button1, Button2, ... ). On click of Button1, Label1 should be displayed in ComboBox, On click of button2, Label2 should be displayed in combobox and so on.

        • 1. Re: ComboBox Manually setting to a particular Item
          Gregory Lafrance Level 6

          There are many ways to actually do this, and the requirements of your applications will guide you in each case, but here is an example:

          <?xml version="1.0"?> 
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
            <mx:Script>
              <![CDATA[
                private function clickFunc(event:MouseEvent):void{
                  switch(event.currentTarget.label){
                    case "Select Label1":
                      cbx.selectedItem = "Label1";
                      break;
                    case "Select Label2":
                      cbx.selectedItem = "Label2";
                      break;
                    case "Select Label3":
                      cbx.selectedItem = "Label3";
                      break;
                    case "Select Label4":
                      cbx.selectedItem = "Label4";
                      break;
                  }
                }
              ]]>
            </mx:Script>
            <mx:ComboBox id="cbx">
              <mx:String>Label1</mx:String>
              <mx:String>Label2</mx:String>
              <mx:String>Label3</mx:String>
              <mx:String>Label4</mx:String>
            </mx:ComboBox>
            <mx:HBox>
              <mx:Button label="Select Label1" click="clickFunc(event);"/>
              <mx:Button label="Select Label2" click="clickFunc(event);"/>
              <mx:Button label="Select Label3" click="clickFunc(event);"/>
              <mx:Button label="Select Label4" click="clickFunc(event);"/>
            </mx:HBox>
          </mx:Application>
          
          • 2. Re: ComboBox Manually setting to a particular Item
            jhapak

            Try,

            combobox.selectedIndex = 1

            combobox.selectedIndex = 2

            • 3. Re: ComboBox Manually setting to a particular Item
              Mohd Fayaz Level 1

              Need some more info.

               

              I am intializing my combobox using the following ArrayCollection

               

                      public var acTestHosts:ArrayCollection = new ArrayCollection([   
                          {label:"Label1",data:2},
                          {label:"Label2",data:3},

                          {label:"Label3",data:3},

                          {label:"Label4",data:3},
                      ]);

               

              and not as you have initialized. Can you please tell me how I can change it to different labels (Label1,Label2,..) using this ArrayCollection as dataprovider to combobox.

              • 4. Re: ComboBox Manually setting to a particular Item
                Mohd Fayaz Level 1

                Please anyone help me on this problem?

                • 5. Re: ComboBox Manually setting to a particular Item
                  Gregory Lafrance Level 6

                  This code also shows how you can access the data field in the dataProvider.

                   

                  <?xml version="1.0"?> 
                  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
                    <mx:Script>
                      <![CDATA[
                        import mx.collections.ArrayCollection;
                      
                        [Bindable] public var acTestHosts:ArrayCollection = new ArrayCollection([   
                          {label:"Label1",data:1},
                          {label:"Label2",data:2},
                          {label:"Label3",data:3},
                          {label:"Label4",data:4},
                        ]);    
                        
                        private function clickFunc(event:MouseEvent):void{
                          switch(event.currentTarget.label){
                            case "Select Label1":
                              cbx.selectedIndex = 0;
                              break;
                            case "Select Label2":
                              cbx.selectedIndex = 1;
                              break;
                            case "Select Label3":
                              cbx.selectedIndex = 2;
                              break;
                            case "Select Label4":
                              cbx.selectedIndex = 3;
                              break;
                          }
                          txt.text = cbx.selectedItem.data;        
                        }
                      ]]>
                    </mx:Script>
                    <mx:ComboBox id="cbx" dataProvider="{acTestHosts}"/>
                    <mx:HBox>
                      <mx:Button label="Select Label1" click="clickFunc(event);"/>
                      <mx:Button label="Select Label2" click="clickFunc(event);"/>
                      <mx:Button label="Select Label3" click="clickFunc(event);"/>
                      <mx:Button label="Select Label4" click="clickFunc(event);"/>
                    </mx:HBox>
                    <mx:TextArea id="txt"/>
                  </mx:Application>
                  
                  • 6. Re: ComboBox Manually setting to a particular Item
                    Mohd Fayaz Level 1

                    That will also not help me.

                     

                    Actually, the data I have shown you is just for illustration. Let me explain you my actual problem.

                    The data in my combobox is dynamic. I have to set the ComboBox based only on the label in ArrayCollection.

                     

                    For example: If ArrayCollection is

                     

                            public var acTestHosts:ArrayCollection = new ArrayCollection([   
                                {label:"John",data:2},
                                {label:"Merry",data:3},

                                {label:"Wilson",data:3},

                                {label:"George",data:3},
                            ]);

                     

                    Then I need something like this

                     

                         var label:String;

                         label="John"

                         cbx.selectedItem = "Merry"  // This should set the combobox to label Merry'

                     

                         label="George"

                         cbx.selectedItem= "George"  //This should set the combobox to label George.

                     

                    The above two assignment to Combobx are not working. Please tell me is there any other way to achieve this.

                    • 7. Re: ComboBox Manually setting to a particular Item
                      Gregory Lafrance Level 6

                      cbx.selectedItem = acTestHosts.getItemAt(acTestHosts.getItemIndex("Merry"));

                      • 8. Re: ComboBox Manually setting to a particular Item
                        Mohd Fayaz Level 1

                        Thanks. This solved my problem.