Code for the above design (Default.aspx)
<body>
<form id="form1" runat="server">
<asp:Button ID="btn2" runat="server" Height="30px"
style="z-index: 1; left: 240px; top: 123px; position: absolute; height: 33px; width: 37px"
Text="2" Width="30px" onclick="btn_Click" />
<asp:TextBox ID="txtValue" runat="server"
style="z-index: 1; left: 191px; top: 86px; position: absolute; width: 184px; text-align:right"></asp:TextBox>
<asp:Button ID="btn3" runat="server" Height="30px"
style="z-index: 1; left: 290px; top: 123px; position: absolute; height: 33px; width: 37px"
Text="3" Width="30px" onclick="btn_Click" />
<asp:Button ID="btn8" runat="server" Height="30px"
style="z-index: 1; left: 240px; top: 215px; position: absolute; height: 33px; width: 37px; bottom: 88px"
Text="8" Width="30px" onclick="btn_Click" />
<asp:Button ID="btnAdd" runat="server" Height="30px"
style="z-index: 1; left: 339px; top: 123px; position: absolute; height: 33px; width: 37px"
Text="+" Width="30px" onclick="btnAdd_Click" />
<asp:Button ID="btn7" runat="server" Height="30px"
style="z-index: 1; left: 190px; top: 215px; position: absolute; height: 33px; width: 37px"
Text="7" Width="30px" onclick="btn_Click" />
<asp:Button ID="btn6" runat="server" Height="30px"
style="z-index: 1; left: 290px; top: 170px; position: absolute; height: 33px; width: 37px"
Text="6" Width="30px" onclick="btn_Click" />
<asp:Button ID="btn5" runat="server" Height="30px"
style="z-index: 1; left: 240px; top: 170px; position: absolute; height: 33px; width: 37px"
Text="5" Width="30px" onclick="btn_Click" />
<asp:Button ID="btn4" runat="server" Height="30px"
style="z-index: 1; left: 190px; top: 170px; position: absolute; height: 33px; width: 37px"
Text="4" Width="30px" onclick="btn_Click" />
<asp:Button ID="btn1" runat="server" Height="30px"
style="z-index: 1; left: 190px; top: 123px; position: absolute; height: 33px; width: 37px; "
Text="1" Width="30px" onclick="btn_Click" />
<asp:Button ID="btn9" runat="server" Height="30px"
style="z-index: 1; left: 290px; top: 215px; position: absolute; height: 33px; width: 37px"
Text="9" Width="30px" onclick="btn_Click" />
<asp:Button ID="btn0" runat="server" Height="30px"
style="z-index: 1; left: 240px; top: 260px; position: absolute; height: 33px; width: 37px; right: 515px;"
Text="0" Width="30px" onclick="btn_Click" />
<asp:Button ID="btnDiv" runat="server" Height="30px"
style="z-index: 1; left: 340px; top: 259px; position: absolute; height: 33px; width: 37px"
Text="/" Width="30px" onclick="btnDiv_Click" />
<asp:Button ID="btnMul" runat="server" Height="30px"
style="z-index: 1; left: 339px; top: 214px; position: absolute; height: 33px; width: 37px"
Text="x" Width="30px" onclick="btnMul_Click" />
<asp:Button ID="btnSub" runat="server" Height="30px"
style="z-index: 1; left: 339px; top: 167px; position: absolute; height: 33px; width: 37px"
Text="-" onclick="btnSub_Click" />
<p>
<asp:Button ID="btnDec" runat="server" Height="30px"
style="z-index: 1; left: 191px; top: 260px; position: absolute; height: 33px; width: 37px"
Text="." Width="30px" onclick="btnDec_Click" />
<asp:Button ID="btnEqual" runat="server" Height="30px"
style="z-index: 1; left: 292px; top: 260px; position: absolute; height: 33px; width: 37px"
Text="=" Width="30px" onclick="btnEqual_Click" />
</p>
<asp:Button ID="btnClose" runat="server" Text="Close"
style="position:absolute; top: 303px; left: 293px; width: 53px; height: 34px;"
onclick="btnClose_Click"/>
<asp:Button ID="btnClear" runat="server" Text="Clear"
style="position:absolute; top: 303px; left: 221px; width: 57px; height: 34px;"
onclick="btnClear_Click"/>
</form>
</body>
C Sharp code for above design(Default.aspx.cs)
//declaring globalvalues
//val for value
Double val=0;
enum operators { Add=1, Sub, Mul,Div, Equal };
//operator_clicked to checked last operator clicked.
int operator_clicked = 0;
//pressed to check for number button pressed
Boolean pressed;
//equal function which is used with every math operator
void equal()
{
//initilizing value of viewstate in val to get the previous value of val
//before clicking any button
val = (double)ViewState["val"];
//fetching value of operator button last clicked in o
int o = (int)ViewState["operator"];
//performing task according to the value of o
switch (o)
{
case (int)operators.Add:
val = (val + Convert.ToDouble(txtValue.Text));
break;
case (int)operators.Sub:
val = (val - double.Parse(txtValue.Text));
break;
case (int)operators.Mul:
val = (val * double.Parse(txtValue.Text));
break;
case (int)operators.Div:
val = (val / double.Parse(txtValue.Text));
break;
default:
if (txtValue.Text != "")
val = Convert.ToDouble(txtValue.Text);
else
val = 0;
break;
}
//assigning textbox text to the val computed above
txtValue.Text = val.ToString();
//storing values in view state.
ViewState["val"] = val;
ViewState["dec_pressed"] = false;
ViewState["pressed"] = false;
}
protected void Page_Load(object sender, EventArgs e)
{
//checking condition whether page is reloaded or not if not reloaded ie. If
//it is loaded for the first time then the below statements will execute.
if (!IsPostBack)
{
//
val = 0;
ViewState["val"] = val;
ViewState["operator"] = 0;
ViewState["pressed"] = false;
ViewState["dec_pressed"] = false;
txtValue.Text = "0";
}
}
//on number button click event this function will execute.
protected void btn_Click(object sender, EventArgs e)
{
//checking whether number button is pressed first time after clicking any
//operator or not
pressed = (Boolean)ViewState["pressed"];
if (pressed==false)
txtValue.Text = "";
ViewState["pressed"] = true;
//concatinating text of button clicked to the text of text box.
txtValue.Text += ((Button)sender).Text;
}
protected void btnAdd_Click(object sender, EventArgs e)
{
equal();
//setting operator clicked value to value of operators.add ie.1 and storing
//in viewstate for further use.
operator_clicked = (int)operators.Add;
ViewState["operator"] = operator_clicked;
}
protected void btnSub_Click(object sender, EventArgs e)
{
equal();
operator_clicked = (int)operators.Sub;
ViewState["operator"] = operator_clicked;
}
protected void btnMul_Click(object sender, EventArgs e)
{
equal();
operator_clicked = (int)operators.Mul;
ViewState["operator"] = operator_clicked;
}
protected void btnDiv_Click(object sender, EventArgs e)
{
equal();
operator_clicked = (int)operators.Div;
ViewState["operator"] = operator_clicked;
}
protected void btnEqual_Click(object sender, EventArgs e)
{
equal();
//setting value of val to 0 and storing in viewstate and operator_clicked
//value to value of equal ie. 5
val = 0;
ViewState["val"] = val;
operator_clicked = (int)operators.Equal;
ViewState["operator"] = operator_clicked;
}
//this will clear text box and assign initial values to viewstate.
protected void btnClear_Click(object sender, EventArgs e)
{
txtValue.Text = "";
ViewState["val"] = 0;
ViewState["operator"]= 0;
ViewState["pressed"]= false;
ViewState["dec_pressed"]= false;
}
protected void btnClose_Click(object sender, EventArgs e)
{
//java script is used to close window.
Response.Write("<script language='javascript'> {window.close();}</script>");
}
//put decimal point in text box if it does not contain any.
protected void btnDec_Click(object sender, EventArgs e)
{
pressed = (Boolean)ViewState["pressed"];
if (pressed == false)
txtValue.Text= "";
//checking whether decimal is ther in text box or not.
if ((bool)ViewState["dec_pressed"] == false)
{
if (txtValue.Text == "")
txtValue.Text = "0.";
else
txtValue.Text += ".";
}
//setting deciaml pressed view satate and number pressed view state to true.
ViewState["dec_pressed"] = true;
ViewState["pressed"] = true;
}
Snap shot
Anonymous User
19-Mar-2019Thanks for the guidance.
Sushant Mishra
16-Jul-2017It was really helpful to read this post.