3 Replies Latest reply on Apr 5, 2011 10:06 AM by Trimakas1

    Assign number variable to text label..

    Trimakas1

      yes I am pretty new to Flex and coding in general.. but I'm trying!!

       

      I have everything working ok, except at the end I want the var z:Number assigned to the resultLabel.text field..

       

      I can calculate z fine.. I just can't get it into resultLabel in order to display it..

       

      Here is my code.. its a simple calculator..

       

      I want to get z into resultLable to display.. any help?

       

       

      <?xml version="1.0" encoding="utf-8"?>
      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                     xmlns:s="library://ns.adobe.com/flex/spark"
                     xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
          <s:layout>
              <s:VerticalLayout gap="10" horizontalAlign="center"/>
          </s:layout>
         
          <fx:Script>
              <![CDATA[
                  private function plus():void{
                      showLabel.text = "+";
                  }
                  private function minus():void{
                      showLabel.text = "-";
                  }
                  private function times():void{
                      showLabel.text = "*";
                  }
                  private function divide():void{
                      showLabel.text = "/";
                  }
                 
                  private function go():void{
                      var x:Number = Number(text1.text);
                      trace("x is: " + x);
                      var y:Number = Number(text2.text);
                      trace("y is: " + y);
                      var z:Number;
                      trace("Z is: " + z);
                      switch(showLabel.text){
                      case "+":
                          z = x+y;
                         
                          trace("Plus Z is " + z);
                          trace("text is " + resultLabel.text);
                          break;
                     
                      case "-":
                          z = x-y;
                          trace("Minus Z is " + z);
                          break;
                     
                      case "*":
                          z = x*y;
                          trace("Times Z is " + z);
                          break;
                     
                      case "/":
                          z = x/y;
                          trace("Div Z is " + z);
                          break;
                     
                      default:
                          break;
                     
                      }
                  }
              ]]>
          </fx:Script>
         
          <fx:Declarations>
              <!-- Place non-visual elements (e.g., services, value objects) here -->
          </fx:Declarations>
         
          <s:HGroup width="436" height="53" gap="10" verticalAlign="top" paddingTop="10">
              <s:TextInput id="text1"/>
              <s:TextArea id="showLabel" width="65" height="22" borderVisible="false" editable="false"
                          focusEnabled="false" textAlign="center"/>
              <s:TextInput id="text2"/>
              <s:Button label="=" id="equalsButton" click= "go()"/>
          </s:HGroup>
          <s:VGroup width="200" height="57" horizontalAlign="center">
              <s:Label id="resultLabel"
                       x="31"
                       width="139"
                       height="38"
                       fontSize="15"/>
          </s:VGroup>
          <s:HGroup width="436" height="200" gap="10" paddingLeft="50">
              <s:Button
                  label="+"
                  click= "plus()"
                  id="addButton"/>
             
              <s:Button
                  label="-"
                  id="subButton"
                  click="minus()"/>
             
              <s:Button
                  label="*"
                  id="multButton"
                  click = "times()"/>
             
              <s:Button
                  label="/"
                  id="divButton"
                  click = "divide()"/>
          </s:HGroup>
      </s:Application>