A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: [as3] combobox data?

  1. #1
    Member
    Join Date
    Nov 2007
    Posts
    36

    [as3] combobox data?

    How do I get the data from a combobox?

  2. #2
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    Here is an example if you want the label:
    PHP Code:
    mc.addItem({label:"item1"});
    mc.addItem({label:"item2"});
    mc.addItem({label:"item3"});
    mc.addEventListener(Event.CHANGEchangeHandler);
    function 
    changeHandler(ev:Event):void
    {
        
    trace(ev.currentTarget.selectedItem.label);

    - The right of the People to create Flash movies shall not be infringed. -

  3. #3
    Member
    Join Date
    Nov 2007
    Posts
    36
    I don't need the label, I need the data

  4. #4
    5+5=55 Schfifty Five's Avatar
    Join Date
    Jun 2006
    Posts
    698
    Then use: trace(ev.currentTarget.selectedItem.data);

  5. #5
    Member
    Join Date
    Nov 2007
    Posts
    36
    It works! Thankyou, thankyou, thankyou!!!
    I could hug you. But you're impossible far away from me for a hug
    Thanks

  6. #6
    Member
    Join Date
    Nov 2007
    Posts
    36
    ... How do I convert the contents of a textfield as an int?

  7. #7
    Member
    Join Date
    Nov 2007
    Posts
    36
    got it
    var number:Number = new Number(txt.text);


  8. #8
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    The "label" is also data.
    - The right of the People to create Flash movies shall not be infringed. -

  9. #9
    Junior Member
    Join Date
    May 2010
    Posts
    21
    I know this is an extremely old thread, but I am trying to do something similar and did not want to start a whole new thread about the same subject, I am trying to get the total amounts from the 4 comboBoxes, into a single text field.

    Here is my code so far:
    Actionscript Code:
    import fl.controls.ComboBox;
    import flash.text.TextField;
    import flash.text.TextFieldType;

    myComboBox.addEventListener(Event.CHANGE, clickHandler);
    myComboBox.addEventListener(Event.CHANGE, totalamount);
     
    var tf:TextField = new TextField();
    addChild(tf);
    tf1.alwaysShowSelection = true;

        function clickHandler(event:Event):void {      
            tf1.text = ComboBox(event.target).selectedItem.data;
    }

    myComboBox2.addEventListener(Event.CHANGE, clickHandler2);
    myComboBox2.addEventListener(Event.CHANGE, totalamount);
    tf2.alwaysShowSelection = true;

        function clickHandler2(event:Event):void {    
            tf2.text = ComboBox(event.target).selectedItem.data;
    }


    myComboBox3.addEventListener(Event.CHANGE, clickHandler3);
    myComboBox3.addEventListener(Event.CHANGE, totalamount);
    tf3.alwaysShowSelection = true;

        function clickHandler3(event:Event):void {    
            tf3.text = ComboBox(event.target).selectedItem.data;
    }


    myComboBox4.addEventListener(Event.CHANGE, clickHandler4);
    myComboBox4.addEventListener(Event.CHANGE, totalamount);
    tf4.alwaysShowSelection = true;

        function clickHandler4(event:Event):void {    
            tf4.text = ComboBox(event.target).selectedItem.data;
    }

    function totalamount(event:Event):void {
           
            total.text = tf1.text + tf2.text + tf3.text + tf4.text;
            total.alwaysShowSelection = true;
    }

    Now, what I have works to get the data, but as you can see, it just adds the text fields and doesn't really do any math to calculate a total amount. I am just learning AS3, from AS2, and I've having a bugger of a time getting it, AS2 was hard enough for me. I'm a designer, not a programmer, but I gotta learn this stuff. Any help is greatly appreciated! Thanks!

  10. #10
    Senior Member joshstrike's Avatar
    Join Date
    Jan 2001
    Location
    Alhama de Granada, España
    Posts
    1,136
    when you get the .text of a textfield, you're getting a string. Using + between strings concatenates them, it doesn't add them as numbers. You need to cast them to Number or int.

    Change the second to last line of your code to
    Actionscript Code:
    total.text = String(Number(tf1.text) + Number(tf2.text) + Number(tf3.text) + Number(tf4.text));
    The Strike Agency
    http://www.theStrikeAgency.com

    StrikeSapphire -- The Original Bitcoin Casino
    https://strikesapphire.com
    (not available in the US)

  11. #11
    Junior Member
    Join Date
    May 2010
    Posts
    21
    Thank you for the response, I changed the code, but I get "NaN" in my totals text field now

    EDIT**
    I got the NAN to go away, basically it was because I had a $ sign in my data field in the ComboBox. But I have another question, if you don't mind- when my totals are added up, i sometimes get 4 or 5 digits behind the decimal (e.g. 35.49999), how can i limit the total to just be, 35.49?

    Thanks!!
    Last edited by nutless_neo; 01-03-2012 at 06:54 PM.

  12. #12
    Junior Member
    Join Date
    Feb 2012
    Posts
    2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center