34 Replies Latest reply on Mar 21, 2011 12:50 PM by kglad-zqbLf4

    Using ComboBox to populate...

    Mr.Wallace Level 1

      Hey folks,

       

      Quick question about the ComboBox component...

       

      If I have a ComboBox component on stage and it has 11 options in it, is it possible to populate a dynamic text field with text based on which one of the 11 options are selected?

       

      Example:

       

      ComboBox has instance name of 'age' and has 11 options to choose from (Opt1, Opt2, Opt3, Opt4, Opt5, etc.).

       

      There are also 11 dynamic text fields (instance and variable names of c1, c2, c3, etc.). I want the text fields to be blank unless the corresponding option is chosen. So, if Opt1 is selected from ComboBox 'age' then populate 'c1' with the number 1, if 'Opt2' is selected populate 'c2' with 2 and so on, otherwise the text fields stay blank.

       

      Make any sense? Possible?

       

      C.

        • 1. Re: Using ComboBox to populate...
          kglad Adobe Community Professional & MVP

          yes, just use a combobox change listener and check the selecteditem's label or data to determine which textfield to populate.

          • 2. Re: Using ComboBox to populate...
            Mr.Wallace Level 1

            That's exactly what I thought it need but I can't find/ don't know the coding necessary to make it work.

             

            C.

            • 3. Re: Using ComboBox to populate...
              Ned Murphy Adobe Community Professional & MVP

              The AS2 Component help documents have examples that cover what you need.  Look up the Combobox.selectedItem property and it has an example that has everything except what you'd need for assigning the textfields the data value.

              1 person found this helpful
              • 4. Re: Using ComboBox to populate...
                Mr.Wallace Level 1

                Ahh, that's what I was looking for!

                 

                Ok, so here is how my code looks and everything works great:

                 

                ---------------------------------------------------------

                age1.addItem({data:1, label:"18-29"});
                age1.addItem({data:2, label:"30"});
                age1.addItem({data:3, label:"31-35"});
                age1.addItem({data:4, label:"36-40"});
                age1.addItem({data:5, label:"41-45"});
                age1.addItem({data:6, label:"46-50"});
                age1.addItem({data:7, label:"51-55"});
                age1.addItem({data:8, label:"56-60"});
                age1.addItem({data:9, label:"61-64"});
                age1.addItem({data:10, label:"65"});
                age1.addItem({data:11, label:"66-69"});
                age1.addItem({data:12, label:"70"});

                 

                var cbListener:Object = new Object();
                cbListener.change = function(evt_obj:Object) {
                var item_obj:Object = age1.selectedItem;
                var i:String;
                for (i in item_obj) {
                  trace(i + ":\t" + item_obj[i]);
                }
                trace("works");
                };
                age1.addEventListener("change", cbListener);

                ---------------------------------------------------------

                 

                So, do I need to add in more code in order to populate a text field now based on a selection from the combobox list?

                 

                C.

                • 5. Re: Using ComboBox to populate...
                  kglad Adobe Community Professional & MVP

                  1.  remove the variable names from your textfields and use their instance names only.

                   


                  age1.addItem({data:1, label:"18-29"});
                  age1.addItem({data:2, label:"30"});
                  age1.addItem({data:3, label:"31-35"});
                  age1.addItem({data:4, label:"36-40"});
                  age1.addItem({data:5, label:"41-45"});
                  age1.addItem({data:6, label:"46-50"});
                  age1.addItem({data:7, label:"51-55"});
                  age1.addItem({data:8, label:"56-60"});
                  age1.addItem({data:9, label:"61-64"});
                  age1.addItem({data:10, label:"65"});
                  age1.addItem({data:11, label:"66-69"});
                  age1.addItem({data:12, label:"70"});

                   

                  var tl:MovieClip=this;

                  var cbListener:Object = new Object();
                  cbListener.change = function(evt_obj:Object) {
                  tl["c"+evt_obj.target.selectedItem.data].text=evt_obj.target.selectedItem.label;
                  };
                  age1.addEventListener("change", cbListener);


                  • 6. Re: Using ComboBox to populate...
                    Mr.Wallace Level 1

                    But, that code I posted works fine.

                     

                    Are you saying to take the variable names off of the text fields I want to be changed by the combobox selection?

                     

                    There will be 18 text fields that will be affected according to which selection is made from the combobox. For example, if '18-29' is selected, then text field 'c171' should populate with the number '1.6'. The other 17 fields should remain blank, then, if a user selects a different age range, like '30', 'c171' should become blank again and 'c172' should popluate with it's number, '1.6'...and so on.

                     

                    Am I biting off more than I can chew?

                     

                    C.

                    • 7. Re: Using ComboBox to populate...
                      Mr.Wallace Level 1

                      Couldn't I just use some sort of "if" statement?

                       

                      Sample (doesn't work):

                       

                      if (age1 = data:1) {
                           c171 = "1.6"
                      } else {
                           c171 = "0";

                      }

                      • 8. Re: Using ComboBox to populate...
                        kglad Adobe Community Professional & MVP

                        assign the data of your combobox items to match the corresponding textfield number and use the code i suggested and remove all textfield variable names.

                        • 9. Re: Using ComboBox to populate...
                          Mr.Wallace Level 1

                          I don't understand...assign the data to match text field?

                           

                          Also, it gives me a syntax error on this line:

                           

                          tl["c"+evt_obj.target.selectedItem.data].text=evt_obj.target.selected Item.label;

                          • 10. Re: Using ComboBox to populate...
                            Mr.Wallace Level 1

                            Well, I'm guessing that the non-reply means this can't be done?

                             

                             

                            • 11. Re: Using ComboBox to populate...
                              kglad Adobe Community Professional & MVP

                              if you have a textfield named c171, one your combobox item's data should be 171 etc

                              • 12. Re: Using ComboBox to populate...
                                Mr.Wallace Level 1

                                Ah ok cool...however I am still getting that syntax error...

                                • 13. Re: Using ComboBox to populate...
                                  Mr.Wallace Level 1

                                  ...dang it...I'm stumped. I cannot for the life of me figure out the error...

                                  • 14. Re: Using ComboBox to populate...
                                    kglad Adobe Community Professional & MVP

                                    copy and paste the error.  we can't see what you see when you test your code.


                                    • 15. Re: Using ComboBox to populate...
                                      Mr.Wallace Level 1

                                      I pasted it in here a couple days ago, but here it is again:

                                       

                                      tl["c"+evt_obj.target.selectedItem.data].text=evt_obj.target.selected Item.label;

                                       

                                      Just says 'syntax error'.

                                       

                                      C.

                                       

                                       

                                       

                                       

                                       

                                      • 16. Re: Using ComboBox to populate...
                                        kglad Adobe Community Professional & MVP

                                        are there any spaces in that line of code?

                                        • 17. Re: Using ComboBox to populate...
                                          Mr.Wallace Level 1

                                          wow, sorry, yes I found the space, here's how things look now:

                                           

                                           

                                          age1.addItem({data:0, label:"age"});
                                          age1.addItem({data:1, label:"18-29"});
                                          age1.addItem({data:2, label:"30"});
                                          age1.addItem({data:3, label:"31-35"});
                                          age1.addItem({data:4, label:"36-40"});
                                          age1.addItem({data:5, label:"41-45"});
                                          age1.addItem({data:6, label:"46-50"});
                                          age1.addItem({data:7, label:"51-55"});
                                          age1.addItem({data:8, label:"56-60"});
                                          age1.addItem({data:9, label:"61-64"});
                                          age1.addItem({data:10, label:"65"});
                                          age1.addItem({data:11, label:"66-69"});
                                          age1.addItem({data:12, label:"70"});

                                           

                                          var tl:MovieClip=this;

                                          var cbListener:Object = new Object();
                                          cbListener.change = function(evt_obj:Object) {
                                          tl["c"+evt_obj.target.selectedItem.data].text=evt_obj.target.selectedItem.label;
                                          };
                                          age1.addEventListener("change", cbListener);

                                           

                                           

                                          As you mentioned earlier in the post, I guess I still have to make the textfield name appear in the data line, right?! So replace data:1 with data:c171 for example, correct?

                                           

                                          What I still don't understand is, how will that popluate c171 with the number I want it to show? So when a user selects 18-29 for example, c171 should display 1.6...

                                          • 18. Re: Using ComboBox to populate...
                                            Mr.Wallace Level 1

                                            wow, sorry, yes I found the space, here's how things look now:

                                             

                                             

                                            age1.addItem({data:0, label:"age"});
                                            age1.addItem({data:1, label:"18-29"});
                                            age1.addItem({data:2, label:"30"});
                                            age1.addItem({data:3, label:"31-35"});
                                            age1.addItem({data:4, label:"36-40"});
                                            age1.addItem({data:5, label:"41-45"});
                                            age1.addItem({data:6, label:"46-50"});
                                            age1.addItem({data:7, label:"51-55"});
                                            age1.addItem({data:8, label:"56-60"});
                                            age1.addItem({data:9, label:"61-64"});
                                            age1.addItem({data:10, label:"65"});
                                            age1.addItem({data:11, label:"66-69"});
                                            age1.addItem({data:12, label:"70"});

                                             

                                            var tl:MovieClip=this;

                                            var cbListener:Object = new Object();
                                            cbListener.change = function(evt_obj:Object) {
                                            tl["c"+evt_obj.target.selectedItem.data].text=evt_obj.target.selectedItem.label;
                                            };
                                            age1.addEventListener("change", cbListener);

                                             

                                             

                                            As you mentioned earlier in the post, I guess I still have to make the textfield name appear in the data line, right?! So replace data:1 with data:c171 for example, correct?

                                             

                                            What I still don't understand is, how will that popluate c171 with the number I want it to show? So when a user selects 18-29 for example, c171 should display 1.6...

                                            • 19. Re: Using ComboBox to populate...
                                              Mr.Wallace Level 1

                                              ...for some reason it's displaying that there is a space in the code on this board but I assure you there is not.

                                              • 20. Re: Using ComboBox to populate...
                                                kglad Adobe Community Professional & MVP

                                                i know, the forum can introduce extraneous spaces.  that's how that space got into my code.

                                                 

                                                use (without extraneous spaces):

                                                 


                                                 

                                                age1.addItem({data:0, label:"age"});
                                                age1.addItem({data:"c171", label:"18-29"});
                                                age1.addItem({data:2, label:"30"});
                                                age1.addItem({data:3, label:"31-35"});
                                                age1.addItem({data:4, label:"36-40"});
                                                age1.addItem({data:5, label:"41-45"});
                                                age1.addItem({data:6, label:"46-50"});
                                                age1.addItem({data:7, label:"51-55"});
                                                age1.addItem({data:8, label:"56-60"});
                                                age1.addItem({data:9, label:"61-64"});
                                                age1.addItem({data:10, label:"65"});
                                                age1.addItem({data:11, label:"66-69"});
                                                age1.addItem({data:12, label:"70"});

                                                 

                                                var tl:MovieClip=this;

                                                var cbListener:Object = new Object();
                                                cbListener.change = function(evt_obj:Object) {
                                                tl[evt_obj.target.selectedItem.data].text=evt_obj.target.selectedItem.label;
                                                };
                                                age1.addEventListener("change", cbListener);

                                                 

                                                 

                                                • 21. Re: Using ComboBox to populate...
                                                  Mr.Wallace Level 1

                                                  Weird.

                                                   

                                                  Anyways, the text field c171 still doesn't populate with anything.

                                                   

                                                  C.

                                                  • 22. Re: Using ComboBox to populate...
                                                    kglad Adobe Community Professional & MVP

                                                    did you remove the variable associated with your textfield and are you sure you have a textfield (on the timeline with that code) with instance name c171?

                                                    • 23. Re: Using ComboBox to populate...
                                                      Mr.Wallace Level 1

                                                      You're going to kill me kglad...

                                                       

                                                      I have found the problem. On another frame above those ones I have other code telling all of the text boxes on the screen to be blank (because there is a 'calculate' button that when pressed populates those fields). I had field c171 on that list, but it's not anymore and it works!

                                                       

                                                      So, my question is, how can I make the combobox read/display 18-29 but when 18-29 is clicked/selected it populates c171 with 1.6, not 18-29?

                                                       

                                                      C.

                                                      • 24. Re: Using ComboBox to populate...
                                                        kglad Adobe Community Professional & MVP

                                                        where's 1.6 coming from?  ie, by what logic will flash know to display 1.6?

                                                        • 25. Re: Using ComboBox to populate...
                                                          Mr.Wallace Level 1

                                                          Well, each age range that is in the combobox represents a rate. So, for example, if they select 18-29, field c171 would populate with 1.6. If they choose 30-34, it shows 1.7, 30 shows 1.8 and so on.

                                                           

                                                          Possible?

                                                           

                                                          C.

                                                          • 26. Re: Using ComboBox to populate...
                                                            kglad Adobe Community Professional & MVP

                                                             


                                                             

                                                            age1.addItem({data:0, label:"age"});
                                                            age1.addItem({data:"c171", label:"18-29"});
                                                            age1.addItem({data:2, label:"30"});
                                                            age1.addItem({data:3, label:"31-35"});
                                                            age1.addItem({data:4, label:"36-40"});
                                                            age1.addItem({data:5, label:"41-45"});
                                                            age1.addItem({data:6, label:"46-50"});
                                                            age1.addItem({data:7, label:"51-55"});
                                                            age1.addItem({data:8, label:"56-60"});
                                                            age1.addItem({data:9, label:"61-64"});
                                                            age1.addItem({data:10, label:"65"});
                                                            age1.addItem({data:11, label:"66-69"});
                                                            age1.addItem({data:12, label:"70"});

                                                             

                                                            var tl:MovieClip=this;

                                                            var cbListener:Object = new Object();
                                                            cbListener.change = function(evt_obj:Object) {
                                                            tl[evt_obj.target.selectedItem.data].text=.1*evt_obj.target.selectedIndex+1.5;
                                                            };
                                                            age1.addEventListener("change", cbListener);

                                                             

                                                             

                                                            1 person found this helpful
                                                            • 27. Re: Using ComboBox to populate...
                                                              Mr.Wallace Level 1

                                                              Ok beautiful, it works! Thank you.

                                                               

                                                              Now my question is (I saw this one coming a mile away)...after I click 18-29 and get 1.6 in field c171, how should the code look for the next age range? That when clicked (ie. 30-34), it repopluates field c171 with a different number, like 1.7?! Doesn't add it or subtract it, just switches it.

                                                               

                                                              Everything I tried so far just gives me syntax errors...

                                                               

                                                              C.

                                                              • 28. Re: Using ComboBox to populate...
                                                                kglad Adobe Community Professional & MVP

                                                                i don't even see a 30-34 range.

                                                                • 29. Re: Using ComboBox to populate...
                                                                  Mr.Wallace Level 1

                                                                  Sorry, 31-34...

                                                                  • 30. Re: Using ComboBox to populate...
                                                                    kglad-zqbLf4 Level 3

                                                                    i don't understand what you're trying to do.

                                                                     

                                                                    but you have several parameters available to use to accomplish whatever you want:

                                                                     

                                                                    1.  you have the data property of each combobox element that you can assign whatever you want.  if you want to use that to reference different textfields, that will work.  if you want to use that to determine what should be in a textfield, that will work.

                                                                     

                                                                    2.  you have the (0-based) index of each combobox element to use.

                                                                     

                                                                    3.  you have the label property of each combobox element.

                                                                     

                                                                    i've shown how to use each of those things in previous messages.

                                                                    • 31. Re: Using ComboBox to populate...
                                                                      Mr.Wallace Level 1

                                                                      Man...you know what...this doesn't seem like it's feasible kglad. I'm basically trying to dusplicate a complex Excel spreadsheet that deals with detailed math formulas over 2 different pages/sheets. The developer can't use the Excel file because the end users aren't allowed to see the math formulas or change them or anything like that. That also means that I can't show the Excel sheet to anybody either as I try to work it out.

                                                                       

                                                                      Without being able to show you what I want to do it is almost impossible for me to describe it all in words.

                                                                       

                                                                      I don't think this is going to be doable...

                                                                       

                                                                      C.

                                                                      • 32. Re: Using ComboBox to populate...
                                                                        kglad-zqbLf4 Level 3

                                                                        i agree.  using a forum for something this complex doesn't seem ideal.

                                                                         

                                                                        if it's important enough and you want to hire me to do this, send an email via my website:  www.kglad.com

                                                                        • 33. Re: Using ComboBox to populate...
                                                                          Mr.Wallace Level 1

                                                                          Sounds good. Thank you so much for the attempted help in this issue, I really do appreciate it!

                                                                           

                                                                          C.

                                                                          • 34. Re: Using ComboBox to populate...
                                                                            kglad-zqbLf4 Level 3

                                                                            you're welcome.