I am making a calculator. It is working fine but when i press on it gives 0 and after it pressing of any digit leads to the value but followed by zero.
I want to overwrite the value on zero on any of the button click on calculator,
Please help me out, its urgent, in addition i am pasting the code here, please do the necessary corrections and send back the code.
Calculator:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
namespace Calculator_full
{
public partial class calculator : System.Web.UI.Page
{
Decimal Num1;
Decimal Num2;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
btn_power.Text = "ON";
btn_power.BackColor = Color.Green;
btn_0.Enabled = false;
btn_1.Enabled = false;
btn_2.Enabled = false;
btn_3.Enabled = false;
btn_4.Enabled = false;
btn_5.Enabled = false;
btn_6.Enabled = false;
btn_7.Enabled = false;
btn_8.Enabled = false;
btn_9.Enabled = false;
btn_clear.Enabled = false;
btn_clearall.Enabled = false;
btn_multiply.Enabled = false;
btn_divide.Enabled = false;
btn_equal.Enabled = false;
btn_add.Enabled = false;
btn_sub.Enabled = false;
}
}
protected void btn_1_Click(object sender, EventArgs e)
{
txt_result.Text = txt_result.Text + 1;
}
protected void btn_8_Click(object sender, EventArgs e)
{
txt_result.Text = txt_result.Text + btn_8.Text;
}
protected void btn_2_Click(object sender, EventArgs e)
{
txt_result.Text = txt_result.Text + 2;
}
protected void btn_3_Click(object sender, EventArgs e)
{
txt_result.Text = txt_result.Text + btn_3.Text;
}
protected void btn_4_Click(object sender, EventArgs e)
{
txt_result.Text = txt_result.Text + btn_4.Text;
}
protected void btn_5_Click(object sender, EventArgs e)
{
txt_result.Text = txt_result.Text + btn_5.Text;
}
protected void btn_6_Click(object sender, EventArgs e)
{
txt_result.Text = txt_result.Text + btn_6.Text;
}
protected void btn_7_Click(object sender, EventArgs e)
{
txt_result.Text = txt_result.Text + btn_7.Text;
}
protected void btn_9_Click(object sender, EventArgs e)
{
txt_result.Text = txt_result.Text + btn_9.Text;
}
protected void btn_0_Click(object sender, EventArgs e)
{
txt_result.Text = txt_result.Text + btn_0.Text;
}
protected void btn_add_Click(object sender, EventArgs e)
{
Num1 = Convert.ToDecimal(txt_result.Text);
Session["first"] = Num1;
Session["Func"] = "ADD";
txt_result.Text = string.Empty;
}
protected void btn_equal_Click(object sender, EventArgs e)
{
Num2 = Convert.ToDecimal(txt_result.Text);
if (Session["Func"].ToString() == "ADD")
{
decimal res = 0;
res = Convert.ToDecimal(Session["First"]) + Num2;
txt_result.Text = String.Format("{0:0.########}", res);
}
else if (Session["Func"].ToString() == "SUB")
{
decimal res = 0;
res = Convert.ToDecimal(Session["First"]) - Num2;
txt_result.Text = String.Format("{0:0.########}", res);
}
else if (Session["Func"].ToString() == "DIV")
{
decimal res = 0;
res = Convert.ToDecimal(Session["First"]) / Num2;
txt_result.Text = String.Format("{0:0.########}", res);
}
else if (Session["Func"].ToString() == "MUL")
{
decimal res = 0;
res = Convert.ToDecimal(Session["First"]) * Num2;
txt_result.Text = String.Format("{0:0.########}", res);
}
}
protected void btn_divide_Click(object sender, EventArgs e)
{
Num1 = Convert.ToDecimal(txt_result.Text);
Session["first"] = Num1;
Session["Func"] = "DIV";
txt_result.Text = string.Empty;
}
protected void btn_multiply_Click(object sender, EventArgs e)
{
Num1 = Convert.ToDecimal(txt_result.Text);
Session["first"] = Num1;
Session["Func"] = "MUL";
txt_result.Text = string.Empty;
}
protected void btn_sub_Click(object sender, EventArgs e)
{
Num1 = Convert.ToDecimal(txt_result.Text);
Session["first"] = Num1;
Session["Func"] = "SUB";
txt_result.Text = string.Empty;
}
protected void btn_clear_Click(object sender, EventArgs e)
{
clearall();
}
protected void btn_clearall_Click(object sender, EventArgs e)
{
clearall();
}
protected void btn_power_Click(object sender, EventArgs e)
{
btn_power.Text = "OFF";
btn_power.BackColor = Color.Red;
btn_0.Enabled = true;
btn_1.Enabled = true;
btn_2.Enabled = true;
btn_3.Enabled = true;
btn_4.Enabled = true;
btn_5.Enabled = true;
btn_6.Enabled = true;
btn_7.Enabled = true;
btn_8.Enabled = true;
btn_9.Enabled = true;
btn_clear.Enabled = true;
btn_clearall.Enabled = true;
btn_multiply.Enabled = true;
btn_divide.Enabled = true;
btn_equal.Enabled = true;
btn_add.Enabled = true;
btn_sub.Enabled = true;
txt_result.Text = txt_result.Text + btn_0.Text ;
Session.Abandon();
}
public void clearall()
{
txt_result.Text = "";
}
protected void txt_result_TextChanged(object sender, EventArgs e)
{
}
protected void btn_decimal_Click(object sender, EventArgs e)
{
txt_result.Text = txt_result.Text + btn_decimal.Text;
}
}