hi, for some few people like accountants they use simple calculators for their tasks everyday. i coded this simple calculator using Vbscript it can be viewed in a sample to try it for free by clicking here. and below you 'll find source code, a screenshot and also the zip file containing the source. F.G. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>A simple Easy to use ASP Calculator</title> </head> <body> <!-- Coded by Réda //--> <script language = "VBscript"><!-- Dim num(7) Dim n Dim ansVal Dim aVal n = -1 Function Math(val) For i = 0 To 7 Step 1 num(i) = "" Next document.f.ans.value = "" n = n + 1 num(n) = val For i = 0 To n Step 1 ansVal = ansVal + num(i) Next document.f.ans.value = ansVal End Function Function equals() aVal = eval(ansVal) MsgBox(aVal) End Function Function Clear() document.f.ans.value = "" For i = 0 To 7 Step 1 num(i) = "" n = -1 ansVal = "" Next End Function //--></script> </head> <body> <form name ="f"> <center style="height: 509px"> <input type = "text" name = "ans" style="width: 97px; height: 22px; margin-left: 0px"> <br /> <br /> <BR> <table> <tr> <td><input type = "button" value = "7" name = "7" onclick = "Call Math('7')" style="height: 95px; width: 65px"></td> <td><input type = "button" value = "8" name = "8" onclick = "Call Math('8')" style="height: 95px; width: 65px"></td> <td> <input type = "button" value = "9" name = "9" onclick = "Call Math('9')" style="height: 95px; width: 65px"></td> <td><input type = "button" value = "/" name = "/" onclick = "Call Math('/')" style="height: 58px; width: 30px"></td> </tr> <tr> <td><input type = "button" value = "4" name = "4" onclick = "Call Math('4')" style="height: 95px; width: 65px"></td> <td><input type = "button" value = "5" name = "5" onclick = "Call Math('5')" style="height: 95px; width: 65px"></td> <td> <input type = "button" value = "6" name = "6" onclick = "Call Math('6')" style="height: 95px; width: 65px"></td> <td><input type = "button" value = "*" name = "*" onclick = "Call Math('*')" style="height: 58px; width: 30px"></td> </tr> <tr> <td><input type = "button" value = "1" name = "1" onclick = "Call Math('1')" style="height: 95px; width: 65px"></td> <td><input type = "button" value = "2" name = "2" onclick = "Call Math('2')" style="height: 95px; width: 65px"></td> <td> <input type = "button" value = "3" name = "3" onclick = "Call Math('3')" style="height: 95px; width: 65px"></td> <td><input type = "button" value = "-" name = "-" onclick = "Call Math('-')" style="height: 58px; width: 30px"></td> </tr> </table> <br /> <input type = "button" value = "+ " name = " +" onclick = "Call Math('+')" style="height: 30px; width: 58px" align="middle"><input type = "button" value = "= " name = " =" onclick = "Call equals" style="height: 30px; width: 179px" align="middle"><br> <br /> <input type = "button" value = " Clear All " onclick = "Call Clear" style="height: 49px; width: 237px"> </center> </form> </body> </html> Code (markup): SCREENSHOT
it's pretty simple to make work put the code in an empty file in visual studio and just click save. don't forget to debug to check if it's working appropriately. i suggest you use an HTML file.
It's coded in client-side VBScript not asp, and will only work in IE, but i guess it is good practice anyway. It's not necessary to run in Visual Studio, just save as HTML file and load in IE. Your better off using javascript for client-side, here is how:. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>A simple Easy to use Javascript calculator</title> <style type="text/css"> input { width: 95px; height: 65px; } #clear { width: 380px; } #equals { width: 285px; } #ans { width: 380px; height: 22px; margin-left: 0px; } #calc { height: 509px; text-align: center; } </style> </head> <body> <script type="text/javascript"> window.onload = function() { var inputs=document.getElementsByTagName("input"); for(i=0; i<inputs.length; i++) { if(inputs[i].getAttribute("type")=="button") inputs[i].onclick = clicked; } } function clicked() { var txt1 = document.getElementById("ans"); if(this.value=="=") alert(eval(txt1.value)); else if(this.value=="Clear All") txt1.value = ""; else txt1.value += this.value; } </script> <div id="calc"> <input type = "text" id = "ans" /><br /><br /><br /> <table> <tr><td> <input type = "button" value = "7" /></td><td> <input type = "button" value = "8" /></td><td> <input type = "button" value = "9" /></td><td> <input type = "button" value = "/" /></td></tr> <tr><td> <input type = "button" value = "4" /></td><td> <input type = "button" value = "5" /></td><td> <input type = "button" value = "6" /></td><td> <input type = "button" value = "*" /></td></tr> <tr><td> <input type = "button" value = "1" /></td><td> <input type = "button" value = "2" /></td><td> <input type = "button" value = "3" /></td><td> <input type = "button" value = "-" /></td></tr> </table> <br /> <input type = "button" value = "+" /> <input type = "button" value = "=" id="equals" /><br /><br /> <input type = "button" value = "Clear All" id="clear"/> </div> </body> </html> Code (markup):
hi, firefox doesn't support client side Vbscript IE which is a product of microsoft thus supports vbscript thus use Internet Explorer (any version) istead
Wow really? I think that i mentioned that about 5-6 months ago... thus vbscript is non-existent for client-side web development. thus use javascript.
if you're using firefox or other browsers(opera safari chrome...) it won't work try to open it in Internet Explorer it is probably available in your coputer unless you removed it using windows componeent tool. camjohnson95 i didn't pay attention to the firefox part in your message sorry
Thanks a lot it worked fine for me .. but there can be a C button which will act like a back space to clear unwanted numbers.
Like Illiana suggested i provide here the new code with backspace key <SCRIPT LANGUAGE="JavaScript"> function addChar(input, character) { if(input.value == null || input.value == "0") input.value = character else input.value += character } function deleteChar(input) { input.value = input.value.substring(0, input.value.length - 1) } function changeSign(input) { if(input.value.substring(0, 1) == "-") input.value = input.value.substring(1, input.value.length) else input.value = "-" + input.value } function compute(form) { form.display.value = eval(form.display.value)} function square(form) { form.display.value = eval(form.display.value) * eval(form.display.value)} function checkNum(str) { for (var i = 0; i < str.length; i++) { var ch = str.substring(i, i+1) if (ch < "0" || ch > "9") { if (ch != "/" && ch != "*" && ch != "+" && ch != "-" && ch != "." && ch != "(" && ch!= ")") { alert("invalid entry!") return false } } } return true } </SCRIPT> </HEAD> <BODY> <!-- Coded by Reda --> <center><form> <input name="display" value="0" size=25> <br> <input type="button" value=" 7 " onClick="addChar(this.form.display, '7')"> <input type="button" value=" 8 " onClick="addChar(this.form.display, '8')"> <input type="button" value=" 9 " onClick="addChar(this.form.display, '9')"> <input type="button" value=" / " onClick="addChar(this.form.display, '/')"> <br> <input type="button" value=" 4 " onClick="addChar(this.form.display, '4')"> <input type="button" value=" 5 " onClick="addChar(this.form.display, '5')"> <input type="button" value=" 6 " onClick="addChar(this.form.display, '6')"> <input type="button" value=" * " onClick="addChar(this.form.display, '*')"> <br> <input type="button" value=" 1 " onClick="addChar(this.form.display, '1')"> <input type="button" value=" 2 " onClick="addChar(this.form.display, '2')"> <input type="button" value=" 3 " onClick="addChar(this.form.display, '3')"> <input type="button" value=" - " onClick="addChar(this.form.display, '-')"> <br> <input type="button" value=" 0 " onClick="addChar(this.form.display, '0')"> <input type="button" value=" . " onClick="addChar(this.form.display, '.')"> <input type="button" value=" +/- " onClick="changeSign(this.form.display)"> <input type="button" value=" + " onClick="addChar(this.form.display, '+')"> <br> <input type="button" value=" Clear " onClick="this.form.display.value = 0 "> <input type="button" value=" Back Space " onClick="deleteChar(this.form.display)"> <input type="button" value=" Enter " name="enter" onClick="if (checkNum(this.form.display.value)) { compute(this.form) }"> </FORM> </BODY> </HTML> Code (markup):