1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Very Simple Calculator In Visual Studio 2008

Discussion in 'C#' started by largn81, May 16, 2009.

  1. #1
    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

    [​IMG]
    SEMrush
     
    largn81, May 16, 2009 IP
    SEMrush
  2. BluSmash12

    BluSmash12 Peon

    Messages:
    154
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    doesnt work for me.
     
    BluSmash12, May 16, 2009 IP
  3. largn81

    largn81 Peon

    Messages:
    237
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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.:p
     
    largn81, May 17, 2009 IP
  4. nikes

    nikes Peon

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    nikes, May 18, 2009 IP
  5. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #5
    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):
     
    camjohnson95, May 18, 2009 IP
  6. largn81

    largn81 Peon

    Messages:
    237
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    this here is the zip file containing the source code :)
     

    Attached Files:

    largn81, Jul 3, 2009 IP
  7. TurkServer

    TurkServer Peon

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    thanks, nice work
     
    TurkServer, Jul 11, 2009 IP
  8. largn81

    largn81 Peon

    Messages:
    237
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    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
     
    largn81, Oct 28, 2009 IP
  9. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #9
    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.
     
    Last edited: Oct 28, 2009
    camjohnson95, Oct 28, 2009 IP
  10. wren

    wren Guest

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    it did not work for me, buttons seem to do nothing
     
    wren, Oct 28, 2009 IP
  11. largn81

    largn81 Peon

    Messages:
    237
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    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
     
    largn81, Oct 29, 2009 IP
  12. illianafrazer

    illianafrazer Active Member

    Messages:
    393
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    63
    #12
    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.
     
    illianafrazer, Nov 3, 2009 IP
  13. chanda

    chanda Peon

    Messages:
    155
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    thanx frnd. . I realy need it
     
    chanda, Aug 5, 2010 IP
  14. nagarajang

    nagarajang Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Anyone provide me code that works in firefox...
     
    nagarajang, Aug 12, 2010 IP
  15. largn81

    largn81 Peon

    Messages:
    237
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    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):
     
    largn81, Oct 29, 2010 IP