3 Replies - 930 Views - Last Post: 18 May 2011 - 07:04 AM Rate Topic: -----

#1 lahrah  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 17-May 11

calculator

Posted 18 May 2011 - 12:13 AM

i jjust started learning c# and i'm working on a calculator.whenever i run my calculator,i'm not able to add or subtract more values eg 2+3+4
    private void button5_Click(object sender, EventArgs e)//add button
        {
            
            sum = sum + double.Parse(textBox1.Text);
            textBox1.Text = textBox1.Text + button5.Text;
            textBox2.Text = textBox1.Text;
            
            

            textBox1.Text = "";

            textBox1.Clear();

            button5clicked = true;
            button4clicked = false;
            button13clicked = false;
            button12clicked = false;
           } 
        
        private void button12_Click(object sender, EventArgs e)//subtract button
        {
            
            sum = sum + double.Parse(textBox1.Text);
            textBox1.Text = textBox1.Text + button12.Text;
            
            textBox2.Text = textBox1.Text;
            
          textBox1.Clear();

            button12clicked = true;
            button5clicked = false;
            button4clicked = false;
            button13clicked = false;

        }

        

        private void Form1_Load(object sender, EventArgs e)
        {
          
        }

        
        private void button15_Click(object sender, EventArgs e)//the equal to button
        {
            if (button4clicked == true)
            {
                add = num1 + num2;
                //textBox1.Text = textBox1.Text + button4.Text;
                //add = sum + double.Parse(textBox1.Text);
                
                //textBox1.Text = add.ToString();
                //sum = 0;
            }
            else if (button5clicked==true)
            {
                
                add = sum - double.Parse(textBox1.Text);
                //textBox2.Text = textBox1.Text;
                textBox1.Text = add.ToString();
                sum = 0;
            }
            
               


Is This A Good Question/Topic? 0
  • +

Replies To: calculator

#2 Servatis  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 394
  • Joined: 30-March 09

Re: calculator

Posted 18 May 2011 - 03:22 AM

First off, give your controls more descriptive names. button15 or textBox2 doesn't tell me anything about what the purpose of the control is. And it makes it very hard to understand what you are trying to do.

From this little bit of code it is hard to give you good advice. Since it looks like half of the relevant code is missing.

It might be a good idea to have a look at one of the tutorials about calculators here on DIC.
Basic Calculator In C#
Was This Post Helpful? 2
  • +
  • -

#3 MATTtheSEAHAWK  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 137
  • View blog
  • Posts: 782
  • Joined: 11-September 10

Re: calculator

Posted 18 May 2011 - 04:48 AM

Well I have a quick suggestion that may not be the best. When I made my first calculator I made two textboxes with one on top of the other and a label in between telling the current operator. The user would fill in both boxes and then press enter. That would make it so the bottom box would fill up with the answer. If you do it this way then you can keep on doing it.

One more way if you only want to have one textbox is that when the user presses an operator you put the current number in a variable and then reset the textbox. After that the user presses a button for an operator and they input one more number. Then the final answer is put in the textbox. Now you can repeat what you did originally and store that number in a variable.

I hope you understood what I was talking about but also like Servatis said, rename your controls to something that's descriptive and if we look at it we will immediately know what it is.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5633
  • View blog
  • Posts: 12,086
  • Joined: 02-June 10

Re: calculator

Posted 18 May 2011 - 07:04 AM

I think you'll find the event driven nature of the Keypad in these tutorials to be very useful in your calculator project.


The tutorials below walk through making an application including inheritance, custom events and custom controls.
Bulding an application - Part 1
Building an application - Part 2
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1