Wednesday, 8 August 2012

Calculator program using asp.net and c#

In this article we are going to build a basic calculator program using c# ans asp.net. We will work with Table,Text-boxes and Button controls to build the application.
First design the calculator

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<style type="text/css">
.tdcss
{
    text-align:right;
}
</style>
    <title>Untitled Page</title>    
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table align="center">
    <tr>  
    <td><asp:TextBox ID="TextBox1" runat="server" ReadOnly="true" CssClass="tdcss" ></asp:TextBox></td>
    </tr>
    </table>
    <table align="center">       
    <tr>
    <td><asp:Button ID="btn1" runat="server" Text="1" onclick="btn1_Click" /></td>
    <td><asp:Button ID="btn2" runat="server" Text="2" onclick="btn2_Click"  /></td>   
    <td><asp:Button ID="btn3" runat="server" Text="3" onclick="btn3_Click"  /></td>
    <td><asp:Button ID="btn4" runat="server" Text="4" onclick="btn4_Click"  /></td>  
    
    </tr>
    <tr>   
    <td><asp:Button ID="btn5" runat="server" Text="5" onclick="btn5_Click" /></td>
    <td><asp:Button ID="btn6" runat="server" Text="6" onclick="btn6_Click" /></td>
    <td><asp:Button ID="btn7" runat="server" Text="7" onclick="btn7_Click"  /></td>
    <td><asp:Button ID="btn8" runat="server" Text="8" onclick="btn8_Click"  /></td>
    
    </tr>
    <tr>       
    <td><asp:Button ID="btn9" runat="server" Text="9" onclick="btn9_Click"  /></td>
    <td><asp:Button ID="btn0" runat="server" Text="0" onclick="btn0_Click"  /></td>
    <td><asp:Button ID="btnplus" runat="server" Text="+" onclick="btnplus_Click"  /></td>     
    <td><asp:Button ID="btnminus" runat="server" Text="-" onclick="btnminus_Click" /></td>
   
   
    </tr>
    <tr align="center">       
      
    <td><asp:Button ID="btnmul" runat="server" Text="*" onclick="btnmul_Click" /></td>
    <td><asp:Button ID="btnmod" runat="server" Text="/" onclick="btnmod_Click" /></td>
    <td><asp:Button ID="btnequal" runat="server" Text="=" onclick="btnequal_Click" /></td>
    <td ><asp:Button ID="btnclear" runat="server" Text="C" OnClick="btnclear_Click" /></td>
    </tr>
    </table>
    </div>
    </form>
</body>
</html>


The code behind the c# :

 static float a, c, d;
    static char b;
    protected void btn1_Click(object sender, EventArgs e)
    {      
        if ((TextBox1.Text == "+") || (TextBox1.Text == "-") || (TextBox1.Text == "*") || (TextBox1.Text == "/"))
        {
            TextBox1.Text = "";
            TextBox1.Text = TextBox1.Text + btn1.Text;
        }
        else
        {
            TextBox1.Text = TextBox1.Text + btn1.Text;
        }
    }
    protected void btn2_Click(object sender, EventArgs e)
    {      
        if ((TextBox1.Text == "+") || (TextBox1.Text == "-") || (TextBox1.Text == "*") || (TextBox1.Text == "/"))
        {
            TextBox1.Text = "";
            TextBox1.Text = TextBox1.Text + btn2.Text;
        }
        else
        {
            TextBox1.Text = TextBox1.Text + btn2.Text;
        }
    }
    protected void btn3_Click(object sender, EventArgs e)
    {      
        if ((TextBox1.Text == "+") || (TextBox1.Text == "-") || (TextBox1.Text == "*") || (TextBox1.Text == "/"))
        {
            TextBox1.Text = "";
            TextBox1.Text = TextBox1.Text + btn3.Text;
        }
        else
        {
            TextBox1.Text = TextBox1.Text + btn3.Text;
        }
    }
    protected void btnplus_Click(object sender, EventArgs e)
    {      
        a = Convert.ToInt32(TextBox1.Text);
        TextBox1.Text = "";
        b = '+';     
        TextBox1.Text += b;
    }
    protected void btn4_Click(object sender, EventArgs e)
    {
        if ((TextBox1.Text == "+") || (TextBox1.Text == "-") || (TextBox1.Text == "*") || (TextBox1.Text == "/"))
        {
            TextBox1.Text = "";
            TextBox1.Text = TextBox1.Text + btn4.Text;
        }
        else
        {
            TextBox1.Text = TextBox1.Text + btn4.Text;
        }
    }
    protected void btn5_Click(object sender, EventArgs e)
    {
        if ((TextBox1.Text == "+") || (TextBox1.Text == "-") || (TextBox1.Text == "*") || (TextBox1.Text == "/"))
        {
            TextBox1.Text = "";
            TextBox1.Text = TextBox1.Text + btn5.Text;
        }
        else
        {
            TextBox1.Text = TextBox1.Text + btn5.Text;
        }
    }
    protected void btn6_Click(object sender, EventArgs e)
    {
        if ((TextBox1.Text == "+") || (TextBox1.Text == "-") || (TextBox1.Text == "*") || (TextBox1.Text == "/"))
        {
            TextBox1.Text = "";
            TextBox1.Text = TextBox1.Text + btn6.Text;
        }
        else
        {
            TextBox1.Text = TextBox1.Text + btn6.Text;
        }
    }
    protected void btnminus_Click(object sender, EventArgs e)
    {
        a = Convert.ToInt32(TextBox1.Text);
        TextBox1.Text = "";
        b = '-';
        TextBox1.Text += b;
    }
    protected void btn7_Click(object sender, EventArgs e)
    {
        if ((TextBox1.Text == "+") || (TextBox1.Text == "-") || (TextBox1.Text == "*") || (TextBox1.Text == "/"))
        {
            TextBox1.Text = "";
            TextBox1.Text = TextBox1.Text + btn7.Text;
        }
        else
        {
            TextBox1.Text = TextBox1.Text + btn7.Text;
        }
    }
    protected void btn8_Click(object sender, EventArgs e)
    {
        if ((TextBox1.Text == "+") || (TextBox1.Text == "-") || (TextBox1.Text == "*") || (TextBox1.Text == "/"))
        {
            TextBox1.Text = "";
            TextBox1.Text = TextBox1.Text + btn8.Text;
        }
        else
        {
            TextBox1.Text = TextBox1.Text + btn8.Text;
        }
    }
    protected void btn9_Click(object sender, EventArgs e)
    {
        if ((TextBox1.Text == "+") || (TextBox1.Text == "-") || (TextBox1.Text == "*") || (TextBox1.Text == "/"))
        {
            TextBox1.Text = "";
            TextBox1.Text = TextBox1.Text + btn9.Text;
        }
        else
        {
            TextBox1.Text = TextBox1.Text + btn9.Text;
        }
    }
    protected void btnmul_Click(object sender, EventArgs e)
    {
       //TextBox1.Text = btnmul.Text;
        a = Convert.ToInt32(TextBox1.Text);
        b = '*';
        TextBox1.Text = "";
        TextBox1.Text += b;
    }
    protected void btn0_Click(object sender, EventArgs e)
    {
        if ((TextBox1.Text == "+") || (TextBox1.Text == "-") || (TextBox1.Text == "*") || (TextBox1.Text == "/"))
        {
            TextBox1.Text = "";
            TextBox1.Text = TextBox1.Text + btn0.Text;
        }
        else
        {
            TextBox1.Text = TextBox1.Text + btn0.Text;
        }
    }
    protected void btnclear_Click(object sender, EventArgs e)
    {
        TextBox1.Text = "";
    }
    protected void btnmod_Click(object sender, EventArgs e)
    {
        a = Convert.ToInt32(TextBox1.Text);
        TextBox1.Text = "";
        b = '/';
        TextBox1.Text += b;
    }
    protected void btnequal_Click(object sender, EventArgs e)
    {
        c = Convert.ToInt32(TextBox1.Text);
        TextBox1.Text = "";
        if (b == '/')
        {
            d = a / c;
            TextBox1.Text += d;
            a = d;
        }
        else if (b == '+')
        {
            d = a + c;
            TextBox1.Text += d;
            a = d;
        }
        else if (b == '-')
        {
            d = a - c;
            TextBox1.Text += d;
            a = d;
        }
        else
        {
            d = a * c;
            TextBox1.Text += d;
            a = d;
        }
    }