Calculator not functioning correctly

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sgmbuk
    New Member
    • Oct 2008
    • 6

    Calculator not functioning correctly

    I have no clue why this is happening.

    The intended behaviour is:
    1. Passing strings of numbers using the numbered buttons (1..0).
    2. Concatenate the strings above into one and converting it into an integer.
    3. Select an operation ( +, -, * and /)
    4. Passing strings of numbers using the numbered buttons (1..0).
    5. Concatenate the strings above into one and converting it into an integer.
    6. Perform the calculation and output the result to the same textbox.

    The actual behaviour:
    1. The program works well with single digits.
    2. The problem starts when I use 2 digit numbers. The first click on the buttons displays the correct number in the text box.
    3. When I click a second time to add a second digit the first value is over written with the new value.
    4. The third click (number selection) adds the intended number correctly.


    Samples of my code that perform the main operations:

    A typical numbered button calls the method constructString :

    Code:
     private void button8_Click(object sender, EventArgs e)
            {
                int j = 2;
                string s = j.ToString(); 
                constructString(s);
            }
    
    constructString builds the string, saves it into the variable nubString and passes it to textbox
    
    private void constructString(String str)
            {
                if (numString == null)
                {
                    numString = str;
    
                }
                else
                {
                    numString = numString + str;
    
                }
                
                textBox1.Text = numString;
            
            }
    This is followed by the following:
    
    private void textBox1_TextChanged(object sender, EventArgs e)
            {
                     if (textBox1.Text != null && numString != null)
                       {
                            if (counter == 0)
                            {
                                v1 = PNNumber(numString);
    
                                //for test purposes
                                textBox3.Text = v1.ToString();
                                numString = null;
                                counter++;
                            }
                            else
                            {
                                v2 = PNNumber(numString);
    
                                // for test purposes
                                textBox3.Text = v2.ToString();
                                numString = null;
    
                                counter = 0;
    
                            }
    
                      }
    
                 
            }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    1.) Please use code tags when posting code.
    2.) Why are you hard coding 2 in the button8_Click method?
    3.) Surely you did not create 10 buttons and ten button_XX methods for each of the buttons?You can create those buttons in a loop and use one method to handle a click on them .
    4.) What does PNNumber do?

    Comment

    • sgmbuk
      New Member
      • Oct 2008
      • 6

      #3
      Hello and thanks for your response. What the PNNumber does the following:

      1, Checks whether the value of the string is NOT null or not.
      2. Converts the string value to integer. Using the Parse method.
      3. Returnds the value

      If the value of the string passed is null you will get the message 'NumString is null' printed into another another textbox.

      Comment

      • sgmbuk
        New Member
        • Oct 2008
        • 6

        #4
        If I was to use a loop to create all the buttons where would I have to call that metho?

        Comment

        Working...