Search This Blog

Wednesday, 18 April 2012

Simple Calculator

Simple Calculator Method 1:


One method of creating simple calculator is given below

Explanation:

First of all the design is very simple consisting of three text boxes and four buttons.

In the code what I have done is:
when you click on Add button, the value from textbox1 and the value from textbox2 is added.
As we know the values of text box is in string we need to convert it to decimal. That is what I have done.
decimal.parse() will convert any value type to decimal.

Also you can observe that I have used if condition for divide click event. This is used because when you divide a number by '0' the result is undefined. 

HTML CODE: 

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Simple Calculator</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
    <tr>
    <td>Enter First Number:</td><td><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
    </tr>
     <tr>
    <td>Enter Second Number:</td><td><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
    </tr>
    </table>  
        <br />
    </div>
        <div>
<!-- double click on Buttons to get OnClick event -->
        <asp:Button ID="BtnAdd" runat="server" Text="Add" Height="30px" Width="100px" OnClick="BtnAdd_Click" />&nbsp;
        <asp:Button ID="BtnSubtract" runat="server" Text="Subtract" Height="30px" Width="100px" OnClick="BtnSubtract_Click"/>&nbsp;
        <asp:Button ID="BtnMultiply" runat="server" Text="Multiply" Height="30px" Width="100px" OnClick="BtnMultiply_Click"/>&nbsp;
        <asp:Button ID="BtnDivide" runat="server" Text="Divide" Height="30px" Width="100px" OnClick="BtnDivide_Click"/>
            <br />
            <br />
        </div>
        <div>
           Result: <asp:TextBox ID="TxtResult" runat="server"></asp:TextBox>
        </div>
    </form>
</body>
</html>

Snapshot of Design :


 

C# CODE : 

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void BtnAdd_Click(object sender, EventArgs e)
    {
        TxtResult.Text = (decimal.Parse(TextBox1.Text) + decimal.Parse(TextBox2.Text)).ToString();
    }
    protected void BtnSubtract_Click(object sender, EventArgs e)
    {
        TxtResult.Text = (decimal.Parse(TextBox1.Text) - decimal.Parse(TextBox2.Text)).ToString();
    }
    protected void BtnMultiply_Click(object sender, EventArgs e)
    {
        TxtResult.Text = (decimal.Parse(TextBox1.Text) * decimal.Parse(TextBox2.Text)).ToString();
    }
    protected void BtnDivide_Click(object sender, EventArgs e)
    {
        if (TextBox2.Text != "0") //This condition is used when we divide by 0 the result is undefined
        {
            TxtResult.Text = (decimal.Parse(TextBox1.Text) / decimal.Parse(TextBox2.Text)).ToString();
        }
        else
        {
            TxtResult.Text = "undefined";
        }
    }
}

 

 



No comments:

Post a Comment