Click here to Skip to main content
12,448,164 members (47,930 online)

How to replace 0 on pressing ON in a calculator

Ravinder Kumar Sharma asked:

Open original thread
I have made a calculator, it is working fine but there is a power button whose text is changed on clicking the button.First it is ON and 0 is written in the result text box. Now i want to replace it with the value on the click of the button, but it is not happening.
Please help me out with this.
There is code also for the convenience as:
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 + btn_2.Text;
            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;
                Convert.ToString(Convert.ToDecimal(Session["First"]) + Num2);
                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")
            {
                Convert.ToString(Convert.ToDecimal(Session["First"]) / Num2);
                decimal res = 0;
                res = Convert.ToDecimal(Session["First"]) / Num2;
                txt_result.Text = String.Format("{0:0.########}", res);
            }
            else if (Session["Func"].ToString() == "MUL")
            {
                Convert.ToString(Convert.ToDecimal(Session["First"]) * Num2);
                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 = "0";
            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;
 
        }
       
    }
}
Please help me out as it is urgent.
Tags: C#, .NET

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).



Advertise | Privacy | Mobile
Web02 | 2.8.160811.3 | Last Updated 26 Mar 2009
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100