Yes, another calculator! The reason why I keep using the same example
(a calculator) in this website is so that you can more easily see the
similarities and differences between different ways of working (VB
project, VBscript, Flash animation...).
And here's the code:
<FORM NAME="Form1">
<input type="text" name="x" size="17">
<input type="radio" name="Operator" value="1" id="add"> +
<input type="radio" name="Operator" value="2" id="subtract"> -
<input type="radio" name="Operator" value="3" id="multiply"> *
<input type="radio" name="Operator" value="4" id="divide"> /
<input type="text" name="y" size="17">
<INPUT TYPE="Button" NAME="Button1" VALUE=" = ">
<input type="text" name="answer" size="17">
</FORM> <SCRIPT FOR="Button1" EVENT="onClick" LANGUAGE="VBScript">
if isnumeric (Form1.x.value) and isnumeric (Form1.y.value) then
x=cdbl(Form1.x.value) ' convert to double precision number format
y=cdbl(Form1.y.value)
if Form1.add.checked = true then ans= x + y
if Form1.subtract.checked = true then ans= x-y
if Form1.multiply.checked = true then ans= x*y
if Form1.divide.checked = true then
if y <> 0 then ans = x/y else ans = "can't divide by zero"
end if
else
ans = "numbers!"
end if
Form1.answer.value = ans
</SCRIPT>
When the button called 'button1' is clicked this event (the onClick
event) triggers the VBscript. The script first checks that both the 'x'
text box and the 'y' text box contain expressions that can converted to
numbers, then converts them into numbers of type 'double precision' (if
the contents of x and y are not specifically converted to numbers then
the '+' operator will 'concatenate' x and y, as in 2+3=23, rather than
adding them as numbers as we want). According to which option button is
selected the answer is calculated and placed in the variable called 'ans'.
As a final step the value of 'ans' is copied into the text box named
'answer'. There is also a check for division by zero and an appropriate
error message.
|