Calculator

I want to create a simple calculator...
I need one text box, and some buttons;

the user enter a number to textbox, and then when user press the + , user can write the second number, and when the user click the = the result will appear in same textbox...
How can I do that

What is the wrong with this code ||=>

string no1 = textBox1.Text;

textBox1.Text = " ";

string no2 = textBox1.Text;

double a = Convert.ToDouble(no1);

double b = Convert.ToDouble(no2);

textBox1.Text = c.ToString();





Answer this question

Calculator

  • Joe Morel - MSFT

    perhaps you need a double c = a + b; right after you declare and give a and b a value

    Also you can use Double.Parse(no1); instead of Convert.ToDouble - going to trust that ToDouble does the same thing as parse, namely parse a string and convert it to its numerical equivalent.

    Also you get a value from textBox1 - clear the value - and then get the second value from the SAME textbox. shouldn't it be textBox2.Text


  • nbhatia

    hi,

    this is a very advanced one but will help you so much

    http://www.personalmicrocosms.com/html/cspcalc.html

    best regards



  • NCGrimbo

    Thank you very much for your help... I found a way...

  • Rockdrala

    If you want to use the same text box, then you need to store a cumulative total every time someone presses the +, and then add that number to the number in the next box the next time the user presses + or =.

    class Calc

    {

    private double total = 0;

    void OnPlusButton(object sender, EventArgs ea) // This needs to be hooked to the + event

    {

    double d =0;

    if (double.TryParse(textBox1.Text, out d)

    {

    total += d;

    }

    textBox1.Text = d.ToString();

    }

    This will keep showing a total every time you press +, you may want to change it to show the total only when you press an = button.



  • Calculator