2 Replies Latest reply on Dec 5, 2006 5:42 PM by moros3

    checkboxes: adding/subtracting values

    moros3 Level 1
      Hi, this is a problem that's been bugging me for a while now.

      There are multiple checkboxes, along with a textarea. How can each checkbox be set with a certain numeric value (ex. price) that, when selected, adds its value to the the textarea? for example:

      user selects checkbox 1: its value (let's say 3.24) its added to the text area. then, user selects checkbox 2. its value is 6.45. this will be the same for every checkbox in the application, it's not just limited to 2 checkboxes. How can the textarea be made to display "9.69" instead of something like 3.246.45?

      Also, how can the app be set so that when the user deselects a checkbox, its value is instead subtracted from the current value of the textarea?

      thanks.

        • 1. Re: checkboxes: adding/subtracting values
          mac_martine
          Here's one way:

          <!-- main.mxml -->

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" xmlns:ns1="*">

          <mx:Script>
          <![CDATA[

          [Bindable]
          private var totalValue:Number = 0;
          private var precision:Number = 4;

          private function itemClicked(event:MouseEvent):void
          {
          var target:CustomCheckbox = CustomCheckbox(event.target);
          var selectedValue:Number = target.selected ? setPrecision(target.value) : setPrecision(target.value * -1);
          totalValue = Number((totalValue + selectedValue).toPrecision(4));
          }
          private function setPrecision(num:Number):Number
          {
          return Number(num.toPrecision(precision));
          }

          ]]>
          </mx:Script>

          <mx:TextInput x="323" y="85" id="total" text="${totalValue}"/>
          <mx:Label x="280" y="87" text="Total:"/>
          <ns1:CustomCheckbox x="126" y="71" label="Apples" value="1.23" click="itemClicked(event)"/>
          <ns1:CustomCheckbox x="126" y="91" label="Oranges" value="4.16" click="itemClicked(event)"/>

          </mx:Application>


          <!-- CustomCheckbox.mxml -->

          <?xml version="1.0" encoding="utf-8"?>
          <mx:CheckBox xmlns:mx=" http://www.adobe.com/2006/mxml">
          <mx:Script>
          <![CDATA[

          private var _value:Number;
          public function set value(v:Number):void { _value = v; }
          public function get value():Number { return _value; }

          ]]>
          </mx:Script>
          </mx:CheckBox>
          • 2. Re: checkboxes: adding/subtracting values
            moros3 Level 1
            Thank you so much!

            Would it be possible to alter the code so that if the number is 3.7, it will display 3.70?