I am trying to develop a C# calculator that can run in the web browser. But it doesn't seem to run. The control goes inside the try function but the variables show 0 or null value.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CalculatorWeb
{
public partial class calc : System.Web.UI.Page
{
double num1 = 0;
double num2 = 0;
double result = 0;
String op;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void clear_Click(object sender, EventArgs e)
{
DisplayBox.Text = "";
}
protected void bspace_Click(object sender, EventArgs e)
{
String str = DisplayBox.Text;
int length;
length = str.Length;
DisplayBox.Text = "";
for (int i = 0; i < length - 1; i++)
{
DisplayBox.Text = DisplayBox.Text + Convert.ToString(str[i]);
}
}
protected void Zero_Click(object sender, EventArgs e)
{
if (DisplayBox.Text != "")
{
DisplayBox.Text = "";
DisplayBox.Text = DisplayBox.Text + Zero.Text;
num2 = double.Parse(Zero.Text);
}
else
{
DisplayBox.Text = DisplayBox.Text + Zero.Text;
}
}
protected void One_Click(object sender, EventArgs e)
{
if (DisplayBox.Text != "")
{
DisplayBox.Text = "";
DisplayBox.Text = DisplayBox.Text + One.Text;
num2 = double.Parse(One.Text);
}
else
{
DisplayBox.Text = DisplayBox.Text + One.Text;
}
}
protected void Two_Click(object sender, EventArgs e)
{
if (DisplayBox.Text != "")
{
DisplayBox.Text = "";
DisplayBox.Text = DisplayBox.Text + Two.Text;
num2 = double.Parse(Two.Text);
}
else
{
DisplayBox.Text = DisplayBox.Text + Two.Text;
}
}
protected void Three_Click(object sender, EventArgs e)
{
if (DisplayBox.Text != "")
{
DisplayBox.Text = "";
DisplayBox.Text = DisplayBox.Text + Three.Text;
num2 = double.Parse(Three.Text);
}
else
{
DisplayBox.Text = DisplayBox.Text + Three.Text;
}
}
protected void Four_Click(object sender, EventArgs e)
{
if (DisplayBox.Text != "")
{
DisplayBox.Text = "";
DisplayBox.Text = DisplayBox.Text + Four.Text;
num2 = double.Parse(Four.Text);
}
else
{
DisplayBox.Text = DisplayBox.Text + Four.Text;
}
}
protected void Five_Click(object sender, EventArgs e)
{
if (DisplayBox.Text != "")
{
DisplayBox.Text = "";
DisplayBox.Text = DisplayBox.Text + Five.Text;
num2 = double.Parse(Five.Text);
}
else
{
DisplayBox.Text = DisplayBox.Text + Five.Text;
}
}
protected void Six_Click(object sender, EventArgs e)
{
if (DisplayBox.Text != "")
{
DisplayBox.Text = "";
DisplayBox.Text = DisplayBox.Text + Six.Text;
num2 = double.Parse(Six.Text);
}
else
{
DisplayBox.Text = DisplayBox.Text + Six.Text;
}
}
protected void Seven_Click(object sender, EventArgs e)
{
if (DisplayBox.Text != "")
{
DisplayBox.Text = "";
DisplayBox.Text = DisplayBox.Text + Seven.Text;
num2 = double.Parse(Seven.Text);
}
else
{
DisplayBox.Text = DisplayBox.Text + Seven.Text;
}
}
protected void Eight_Click(object sender, EventArgs e)
{
if (DisplayBox.Text != "")
{
DisplayBox.Text = "";
DisplayBox.Text = DisplayBox.Text + Eight.Text;
num2 = double.Parse(Eight.Text);
}
else
{
DisplayBox.Text = DisplayBox.Text + Eight.Text;
}
}
protected void Nine_Click(object sender, EventArgs e)
{
if (DisplayBox.Text != "")
{
DisplayBox.Text = "";
DisplayBox.Text = DisplayBox.Text + Nine.Text;
num2 = double.Parse(Nine.Text);
}
else
{
DisplayBox.Text = DisplayBox.Text + Nine.Text;
}
}
protected void Add_Click(object sender, EventArgs e)
{
if (DisplayBox.Text == "")
{
Response.Write(" Enter a number first");
}
else
{
num1 = double.Parse(DisplayBox.Text);
DisplayBox.Text = Add.Text;
op = "plus";
}
}
protected void Sub_Click(object sender, EventArgs e)
{
if (DisplayBox.Text == "")
{
Response.Write(" Enter a number first");
}
else
{
num1 = double.Parse(DisplayBox.Text);
DisplayBox.Text = Sub.Text;
op = "minus";
}
}
protected void Mul_Click(object sender, EventArgs e)
{
if (DisplayBox.Text == "")
{
Response.Write(" Enter a number first");
}
else
{
num1 = double.Parse(DisplayBox.Text);
DisplayBox.Text = Mul.Text;
op = "mul";
}
}
protected void Div_Click(object sender, EventArgs e)
{
if (DisplayBox.Text == "")
{
Response.Write(" Enter a number first");
}
else
{
num1 = double.Parse(DisplayBox.Text);
DisplayBox.Text = Div.Text;
op = "div";
}
}
protected void equal_Click(object sender, EventArgs e)
{
if (DisplayBox.Text == "" || DisplayBox.Text.Equals(op))
{
Response.Write("Enter a number");
}
else
{
//num2 = double.Parse(DisplayBox.Text);
try
{
Response.Write("inside try");
if (op == "plus")
{
Response.Write("inside add");
result = num1 + num2;
DisplayBox.Text = Convert.ToString(result);
}
if (op == "minus")
{
result = num1 - num2;
DisplayBox.Text = Convert.ToString(result);
}
if (op == "mul")
{
result = num1 * num2;
DisplayBox.Text = Convert.ToString(result);
}
if (op == "div")
{
result = num1 / num2;
DisplayBox.Text = Convert.ToString(result);
}
}
catch (Exception)
{ }
}
}
protected void deci_Click(object sender, EventArgs e)
{
DisplayBox.Text = DisplayBox.Text + deci.Text;
}
protected void DisplayBox_TextChanged(object sender, EventArgs e)
{
}
}
}