Add last input value in result value on clicking result button ? [Resolved]

Posted by Adeel under C# on 7/31/2015 | Points: 10 | Views : 455 | Status : [Member] | Replies : 2
i have textbox. Two values and a operator is entered in textbox. after clicking result button answer is displayed in textbox. What i want once result is calculated and displayed in textbox, then if user again click result button then it must add result value to last entered input in textbox.




Responses

Posted by: Rajnilari2015 on: 8/3/2016 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
So you are trying to make a mini calculator

Here is what you can do.

public string Calculate(string firstNumber, string secondNumber, string Operator)
{
string result = string.Empty;

try
{
switch (Operator)
{
case "+": result = (float.Parse(firstNumber) + float.Parse(secondNumber)).ToString(); break;
case "-": result = (float.Parse(firstNumber) - float.Parse(secondNumber)).ToString(); break;
case "*": result = (float.Parse(firstNumber) * float.Parse(secondNumber)).ToString(); break;
case "/": result = (float.Parse(firstNumber) / float.Parse(secondNumber)).ToString(); break;
}
}
catch (Exception ex)
{
throw ex;

}
return result;
}


Earlier we have developed a similar kind of application which is avaliable at ( http://www.dotnetfunda.com/articles/show/1650/building-a-calculator-using-wp-71mango )

You can download that and reuse the class files. Even the piece of code posted here is adopted from that article.

Hope that answers your question.

--
Thanks & Regards,
RNA Team

Adeel, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ugrasen on: 8/2/2016 [Member] Starter | Points: 25

Up
0
Down
if (textBox4.Text=="")

{
int a = Int32.Parse(textBox1.Text);
int b = Int32.Parse(textBox2.Text);
if ("+" == textBox3.Text)
{
textBox4.Text = (a + b).ToString();

}
else if ("-" == textBox3.Text)
{
textBox4.Text = (a - b).ToString();
}
else if ("*" == textBox3.Text)
{
textBox4.Text = (a * b).ToString();
}
else if ("/" == textBox3.Text)
{
textBox4.Text = (a / b).ToString();
}
}
else
{
if ("+" == textBox3.Text)
{
textBox4.Text = (Int32.Parse(textBox4.Text) + Int32.Parse(textBox4.Text)).ToString();
}
else if ("-" == textBox3.Text)
{
textBox4.Text = (Int32.Parse(textBox4.Text) - Int32.Parse(textBox4.Text)).ToString();
}
else if ("*" == textBox3.Text)
{
textBox4.Text = (Int32.Parse(textBox4.Text) * Int32.Parse(textBox4.Text)).ToString();
}
else if ("/" == textBox3.Text)
{
textBox4.Text = (Int32.Parse(textBox4.Text) / Int32.Parse(textBox4.Text)).ToString();
}
}


Adeel, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response