Click here to Skip to main content
15,910,981 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:

C#
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
    {
        //float a;
        //float b;
        //float c;
        //bool plus = false;
        //bool minus = false;
        //bool multiply = false;
        //bool divide = false;
        Decimal Num1;
        Decimal Num2;
       // string Symbol;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
                btn_power.Text = "ON";

                btn_power.BackColor = Color.Green;


                //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_power.Enabled = true;
                //btn_multiply.Enabled = true;
                //btn_divide.Enabled = true;
                //btn_equal.Enabled = true;
                //btn_add.Enabled = true;
                //btn_sub.Enabled = true;

                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;

               // txt_result.Text = txt_result.Text + btn_0.Text;
            }
        }



        protected void btn_1_Click(object sender, EventArgs e)
        {
            //txt_result.Text = txt_result.Text + btn_1.Text;

            txt_result.Text = txt_result.Text + 1;
            //Num1 = Convert.ToInt32(txt_result.Text);

        }

        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)
        {
            //txt_result.Text = txt_result.Text + btn_add.Text; 
            //a = Convert.ToInt32(txt_result.Text);
            //// txt_result.Text = "";
            //txt_result.Text += btn_add.Text;
            //plus = true;

            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)
        {
            //b = Convert.ToInt32(txt_result.Text);
            //if (plus == true)
            //{
            //    c = Convert.ToInt32(txt_result.Text);
            //    txt_result.Text = Convert.ToString(c);
            //}
            //else if (minus == true)
            //{
            //    c = a - b;
            //    txt_result.Text = Convert.ToString(c);
            //}
            //else if (multiply == true)
            //{
            //    c = a * b;
            //    txt_result.Text = Convert.ToString(c);
            //}
            //else if (divide == true)
            //{
            //    c = a / b;
            //    txt_result.Text = Convert.ToString(c);
            //}
            //else
            //{
            //    txt_result.Text = "";
            //}

            Num2 = Convert.ToDecimal(txt_result.Text);
            if (Session["Func"].ToString() == "ADD")
            {
                decimal res = 0;
                //txt_result.Text = 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")
            {
                //txt_result.Text = 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")
            {
                //txt_result.Text = 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 (Symbol == "*")
            //{
            //    txt_result.Text = Num1 * Num2;
            //}
            //else if (Symbol == "/")   
            //{
            //    txt_result.Text = Num1 / Num2;
            //}


        }

    

        protected void btn_divide_Click(object sender, EventArgs e)
        {
            ////txt_result.Text = txt_result.Text + btn_divide.Text; 
            //a = Convert.ToInt32(txt_result.Text);
            //txt_result.Text = "";
            //divide = true;
            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)
        {
            ////txt_result.Text = txt_result.Text + btn_multiply.Text; 
            //a = Convert.ToInt32(txt_result.Text);
            //txt_result.Text = "";
            //multiply = true;
            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)
        {
            ////txt_result.Text = txt_result.Text + btn_sub.Text; 
            //a = Convert.ToInt32(txt_result.Text);
            //txt_result.Text = "";
            //minus = true;

            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)
        {
            //if (txt_result.Text = txt_result.Text,
            //    txt_result.Text = txt_result.Text + btn_0.Text);
            //else txt_result.Text = "";
            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_power.Enabled = true;
            btn_multiply.Enabled = true;
            btn_divide.Enabled = true;
            btn_equal.Enabled = true;
            btn_add.Enabled = true;
            btn_sub.Enabled = true;

            //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;

            //txt_result.Text = "";
            //a = 0;
            //b = 0;
            //c = 0;
            //plus = false;
            //minus = false;
            //multiply = false;
            //divide = false;


           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;
        }
        //public override string txt_result.text;
        //{
        //    get { return txt_result.Text.Replace(",", ""); }
        //    set { txt_result.Text = value; }
        //}
    }
Posted
Updated 30-Jan-13 23:39pm
v2

Change the following code
C#
protected void btn_decimal_Click(object sender, EventArgs e)
{
txt_result.Text = txt_result.Text + btn_decimal.Text;
}


to

C#
protected void btn_decimal_Click(object sender, EventArgs e)
{
if(txt_result.Text=="0")
{
   txt_result.Text = btn_decimal.Text;
}
else
{
   txt_result.Text = txt_result.Text + btn_decimal.Text;
}
}
 
Share this answer
 
Comments
Ravinder Kumar Sharma 31-Jan-13 6:24am    
Please read the code carefully and then understand the help that I need.

I want that there is zero in the result textbox when I press ON.
After this I want that now the value that goes in the textbox must replace 0 that we get on pressing ON and on the same button the text is changed i.e. afer pressing ON the text on the same button changes to Off. So, i want clear textbox on pressing OFF, Please help me out.
Why don't you try checking the length of the result text as well as its value.
Try the following modification on button click it might solve your problem

C#
protected void btn_1_Click(object sender, EventArgs e)
        {
            //txt_result.Text = txt_result.Text + btn_1.Text;
            if(txt_result.Text.length == 1 && txt_result.Text == "0")
            {
               txt_result.Text = "1";
            }
            else
            {
               txt_result.Text = txt_result.Text + 1;
               //Num1 = Convert.ToInt32(txt_result.Text);
            }
        }
 
Share this answer
 
Comments
Ravinder Kumar Sharma 31-Jan-13 6:26am    
Thanx but you dont understand dear, for this purpose i already have button for 1 actually i need to replace the value to any digit on click after pressing ON
T Pat 31-Jan-13 6:57am    
So you actually want replace the initial '0' value after ON with any digit you press first, is that right?
Well..in this case you can just have one event for all your number text boxes or you can replace all the button event code as below whereever applicable

C#
protected void numberbutton_Click(object sender, EventArgs e)
 {
  if(txt_result.Text=="0")
   {
    txt_result.Text = sender.Text;
   }
 else
 {
   txt_result.Text = txt_result.Text + sender.Text;
 }
}


Typecase the sender to button if required.
 
Share this answer
 
JUST WRITE THE FOLLOWING CODE IT WILL WORK FINE..........

1.CHOOSE YOUR EVENT LIKE PAGE LOAD,TEXTCHANGED,BUTTON CLICK WHICH IS RIGHT 4 U
2. (I HAVE TAKEN TEXT CHANGED EVENT)
3.
PUT THE CODE
if ( TextBox1.Text.StartsWith(Convert.ToString(0)))
{
TextBox1.Text = TextBox1.Text.Remove(0,1);


}
 
Share this answer
 
Comments
clivethescott 10-Feb-13 17:03pm    
why call Convert.ToString(0) when you can just use the string "0". And calling Remove and then eventually inserting the pressed button text looks like doing double the job

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900