Tuesday, August 16, 2011

Calculator with Flex4

Calculator is an example of event handling using Flex4. It can make some simple mathematical calculations like addition, subtraction, division and multiplication. the interface of that calculator is as follow


The functions used in the logic of that calculator are implemented and used to handle the click events for any button of that calculator buttons as the following:

<s:Button x="25" y="164" label="1" width="29" height="31" click="calculateResult(1)"/>
<s:Button x="187" y="67" label="*" fontFamily="Times New Roman" fontSize="23" height="27" width="67" fontWeight="normal" fontStyle="italic" textDecoration="none" lineThrough="false" focusColor="#345471" enabled="true" id="multiply" click="changeSign("*")"/>


<fx:Script>
private var calcResult:int = 0;
private var sign:String = "";
private function calculateResult( newNum:int):void
{
var firstNum: int = int(parseInt(result.text, 10));
if (sign == "*")
{
calcResult = firstNum*newNum;
sign = "";
}
else if (sign == "-")
{
calcResult = firstNum-newNum;
sign = "";
}
else if (sign == "+")
{
calcResult = firstNum+newNum;
sign = "";
}
else if (sign == "/")
{
calcResult = firstNum/newNum;
sign = "";
}
else
{
calcResult = newNum;
}
result.text = calcResult.toString();
}

private function changeSign(newSign : String):void
{
if (newSign == "clr")
{
calcResult = 0;
result.text = calcResult.toString();
sign = "";
}
else
sign = newSign;
}
</fx:Script>

To download the full source code click here

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.