Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I'm asked to press "0" button and it remains 0 instead of keeping increase "000000". What should I add in this code?
C#
<pre>#region EQUAL CALCULATION
        protected void Equal_Click(object sender, EventArgs e)
        {
            decimal output;
           
            //Check whether it has data or not
            try
            {
                Expression a = new Expression(lblExpression.Text);
                if (decimal.TryParse(a.Evaluate().ToString(), out output))
                {
                    lblResult.Text = output.ToString();
                    lblExpression.Text = "";
                }
            }
            catch (Exception)
            {
                lblExpression.Text =  lblExpression.Text;
                lblResult.Text = "Invalid Calculation";

            }
            
        }
        #endregion

        #region BUTTON FUNCTION
        protected void Btn1_Click(object sender, EventArgs e)
        {
            lblExpression.Text = lblExpression.Text + button1.Text;
        }

        protected void Btn2_Click(object sender, EventArgs e)
        {
            lblExpression.Text = lblExpression.Text + button2.Text;
        }

        protected void Btn3_Click(object sender, EventArgs e)
        {
            lblExpression.Text = lblExpression.Text + button3.Text;
        }

        protected void Btn4_Click(object sender, EventArgs e)
        {
            lblExpression.Text = lblExpression.Text + button4.Text;
        }

        protected void Btn5_Click(object sender, EventArgs e)
        {
            lblExpression.Text = lblExpression.Text + button5.Text;
        }

        protected void Btn6_Click(object sender, EventArgs e)
        {
            lblExpression.Text = lblExpression.Text + button6.Text;
        }

        protected void Btn7_Click(object sender, EventArgs e)
        {
            lblExpression.Text = lblExpression.Text + button7.Text;
        }

        protected void Btn8_Click(object sender, EventArgs e)
        {
            lblExpression.Text = lblExpression.Text + button8.Text;
        }

        protected void Btn9_Click(object sender, EventArgs e)
        {
            lblExpression.Text = lblExpression.Text + button9.Text;
        }

        protected void Btn0_Click(object sender, EventArgs e)
        {
            lblExpression.Text = lblExpression.Text + button0.Text;
        }

        #endregion

        #region OPERATOR 
        protected void Decimal_Click(object sender, EventArgs e)
        {
            lblExpression.Text = lblExpression.Text + Decimal.Text;
        }

        protected void Plus_Click(object sender, EventArgs e)
        {
            lblExpression.Text = lblExpression.Text + Plus.Text;
        }

        protected void Minus_Click(object sender, EventArgs e)
        {
            lblExpression.Text = lblExpression.Text + Minus.Text;
        }

        protected void Divide_Click(object sender, EventArgs e)
        {
            lblExpression.Text = lblExpression.Text + Divide.Text;
        }

        protected void Multiply_Click(object sender, EventArgs e)
        {
            lblExpression.Text = lblExpression.Text + Multiply.Text;
        }
        #endregion

        protected void Reset_Click(object sender, EventArgs e)
        {
            lblExpression.Text = "";
            lblResult.Text = "0";

        }

        protected void Back_Click(object sender, EventArgs e)
        {
            //remove by 1 integer
            string s = "";
            if (lblExpression.Text.Length > 1)
            {
                s = lblExpression.Text;
                s = s.Substring(0, s.Length- 1);
            }
            lblExpression.Text = s;
        }


What I have tried:

I don't know where should i add
Posted
Updated 3-Aug-18 3:43am
Comments
F-ES Sitecore 3-Aug-18 4:33am    
Think about it in terms of algorithms, or pseudocode. What you have is

when button0 clicked
output = output + "0"
end when

So when you click 0 three times you get "000" what you want though is more like

when button0 clicked
if output is not "0" then
output = output + "0"
end if
end when

That way when you press "0" once you get "0", but when you press it again the output is already "0" so nothing happens. You just need to turn that into code, and don't ask me to do it for you, we're not here to do your homework.
ZurdoDev 3-Aug-18 8:09am    
Please post as solution.
Richard Deeming 3-Aug-18 13:11pm    
You can combine a lot of those event handlers, rather than having lots of duplicated code:
protected void Button_Click(object sender, EventArgs e)
{
    var button = (Button)sender;
    lblExpression.Text = lblExpression.Text + button.Text;
}

1 solution

using Microsoft.VisualBasic

you can use VAL() function to avoid above problem.
 
Share this answer
 

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