Steps to create calculator using visual studio 2012

  • Step 1: Open Visual Studio,
  • Step 2: Then click on New Project OR Go to File > New > Project
  • Step 3: In New Project page you will see dropdown option on the hand left side, in that go to Installed > Templates > Visual C# > Web. After that select ASP.NET Empty Web Application.

Step3

  • Step 4: After selecting ASP.NET Empty Web Application write the Name & Solution Name as Calc and set the Location, as shown in above figure.
  • Step 5: Now open Solution Explore in that right click on solution file Calc and goto Add and select New Item

step5

 

  • Step 6: In Add New Item page select Web Form and name it as Calc.aspx

step6

  • Step 7: Now expand Calc.aspx in Solution Explorer you will see Calc.aspx.cs

step7

  • Step 8: Paste the following code in Calc.aspx.cs and Calc.aspx accordingly.
  • Step 9: Run the program

 


Calc.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Calc
{
public partial class Calc : System.Web.UI.Page
{
static float a, c, d;
static char b;
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Bt0_Click(object sender, EventArgs e)
{
if ((Tb1.Text == “+”) || (Tb1.Text == “-“) || (Tb1.Text == “*”) || (Tb1.Text == “/”))
{
Tb1.Text = “”;

Tb1.Text = Tb1.Text + Bt0.Text;
}
else
{
Tb1.Text = Tb1.Text + Bt0.Text;

}
}

protected void Bt1_Click(object sender, EventArgs e)
{
if ((Tb1.Text == “+”) || (Tb1.Text == “-“) || (Tb1.Text == “*”) || (Tb1.Text == “/”))
{
Tb1.Text = “”;

Tb1.Text = Tb1.Text + Bt1.Text;
}
else
{
Tb1.Text = Tb1.Text + Bt1.Text;

}

}

protected void Bt2_Click(object sender, EventArgs e)
{
if ((Tb1.Text == “+”) || (Tb1.Text == “-“) || (Tb1.Text == “*”) || (Tb1.Text == “/”))
{
Tb1.Text = “”;

Tb1.Text = Tb1.Text + Bt2.Text;
}
else
{
Tb1.Text = Tb1.Text + Bt2.Text;

}
}

protected void Bt3_Click(object sender, EventArgs e)
{
if ((Tb1.Text == “+”) || (Tb1.Text == “-“) || (Tb1.Text == “*”) || (Tb1.Text == “/”))
{
Tb1.Text = “”;

Tb1.Text = Tb1.Text + Bt3.Text;
}
else
{
Tb1.Text = Tb1.Text + Bt3.Text;

}
}

protected void Bt4_Click(object sender, EventArgs e)
{
if ((Tb1.Text == “+”) || (Tb1.Text == “-“) || (Tb1.Text == “*”) || (Tb1.Text == “/”))
{
Tb1.Text = “”;

Tb1.Text = Tb1.Text + Bt4.Text;
}
else
{
Tb1.Text = Tb1.Text + Bt4.Text;

}
}

protected void Bt5_Click(object sender, EventArgs e)
{
if ((Tb1.Text == “+”) || (Tb1.Text == “-“) || (Tb1.Text == “*”) || (Tb1.Text == “/”))
{
Tb1.Text = “”;

Tb1.Text = Tb1.Text + Bt5.Text;
}
else
{
Tb1.Text = Tb1.Text + Bt5.Text;

}
}

protected void Bt6_Click(object sender, EventArgs e)
{
if ((Tb1.Text == “+”) || (Tb1.Text == “-“) || (Tb1.Text == “*”) || (Tb1.Text == “/”))
{
Tb1.Text = “”;

Tb1.Text = Tb1.Text + Bt6.Text;
}
else
{
Tb1.Text = Tb1.Text + Bt6.Text;

}
}

protected void Bt7_Click(object sender, EventArgs e)
{
if ((Tb1.Text == “+”) || (Tb1.Text == “-“) || (Tb1.Text == “*”) || (Tb1.Text == “/”))
{
Tb1.Text = “”;

Tb1.Text = Tb1.Text + Bt7.Text;
}
else
{
Tb1.Text = Tb1.Text + Bt7.Text;

}
}

protected void Bt8_Click(object sender, EventArgs e)
{
if ((Tb1.Text == “+”) || (Tb1.Text == “-“) || (Tb1.Text == “*”) || (Tb1.Text == “/”))
{
Tb1.Text = “”;

Tb1.Text = Tb1.Text + Bt8.Text;
}
else
{
Tb1.Text = Tb1.Text + Bt8.Text;

}
}

protected void Bt9_Click(object sender, EventArgs e)
{
if ((Tb1.Text == “+”) || (Tb1.Text == “-“) || (Tb1.Text == “*”) || (Tb1.Text == “/”))
{
Tb1.Text = “”;

Tb1.Text = Tb1.Text + Bt9.Text;
}
else
{
Tb1.Text = Tb1.Text + Bt9.Text;

}
}

public void Bta_Click(object sender, EventArgs e)
{

a = Convert.ToInt32(Tb1.Text);
Tb1.Text = “”;
b = ‘+’;
Tb1.Text = Tb1.Text + b;

}

protected void Bts_Click(object sender, EventArgs e)
{
a = Convert.ToInt32(Tb1.Text);
Tb1.Text = “”;
b = ‘-‘;
Tb1.Text = Tb1.Text + b;
}

protected void Btd_Click(object sender, EventArgs e)
{
a = Convert.ToInt32(Tb1.Text);
Tb1.Text = “”;
b = ‘/’;
Tb1.Text = Tb1.Text + b;
}

protected void Btm_Click(object sender, EventArgs e)
{
a = Convert.ToInt32(Tb1.Text);
b = ‘*’;
Tb1.Text = “”;
Tb1.Text = Tb1.Text + b;
}

protected void Btdot_Click(object sender, EventArgs e)
{
Tb1.Text += “.”;
}

protected void Bte_Click(object sender, EventArgs e)
{
int c = Convert.ToInt32(Tb1.Text);
Tb1.Text = “”;
if (b == ‘+’)
{
d = a + c;
Tb1.Text = Tb1.Text + d;
a = d;
}
else if (b == ‘-‘)
{
d = a – c;
Tb1.Text = Tb1.Text + d;
a = d;
}

else if (b == ‘*’)
{
d = a * c;
Tb1.Text = Tb1.Text + d;
a = d;
}

else if (b == ‘/’)
{
d = a / c;
Tb1.Text = Tb1.Text + d;
a = d;
}

}

protected void Btc_Click(object sender, EventArgs e)
{
Tb1.Text = ” “;
}

}
}


Calc.aspx

<%@ Page Language=”C#” AutoEventWireup=”true” CodeBehind=”Calc.aspx.cs” Inherits=”Calc.Calc” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;

<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head id=”Head1″ runat=”server”>
<title></title>
</head>
<body>
<form id=”form1″ runat=”server”>

</form>
</body>
</html>


Output

calc


Here is the pdf file for above program.

Calc.aspx.cs

Calc.aspx

Advertisements