Question calculator textbox doesn't display it

ithirdy

New member
Joined
Dec 26, 2012
Messages
1
Programming Experience
1-3
Hi everyone, i am making this calculator and i already have a source code which i will post later. I have two textboxes, the 1st one(TextBox1) displays the answer while the 2nd one(TextBox2) shall display the equation. The problem is, The 2nd textbox doesn't display it. for example,
1. Click "1", TextBox1 and TextBox2 Displays " 1 "
2. Click "+" TextBox1 becomes empty, TextBox2 displays " 1 + "
3. Click "2", TextBox1 displays " 2 ", TextBox2 displays " 1 + 2 "
4. Click "+" again, TextBox1 displays " 3 ", TextBox2 displays " 3 + " but TextBox2 should have been displaying " 1 + 2 + "

So how am I going to display all the equation or the input on TextBox2 while the answers are on TextBox1???

Help me out please. Thank you very much :)

This is the code i found online which i manipulated a bit.

Code:
Public Class Calculator


    Dim total1 As Double
    Dim total2 As Double
    'variables to hold operands
    Private valHolder1 As Double
    Private valHolder2 As Double
    'Variable to hold temporary values
    Private tmpValue As Double
    'True if "." is use else false
    Private hasDecimal As Boolean
    Private inputStatus As Boolean
    Private clearText As Boolean
    'variable to hold Operater
    Private calcFunc As String


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Check the inputStatus
        If inputStatus Then 'Its True
            'Append values to the value
            'in the input box
            TextBox1.Text += Button1.Text
            TextBox2.Text += Button1.Text
        Else   'Value is False
            'Set the value to the value of the button
            TextBox1.Text = Button1.Text
            TextBox2.Text += Button1.Text
            'Toggle inputStatus to True
            inputStatus = True
        End If
    End Sub




    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If inputStatus Then
            TextBox1.Text += Button2.Text
            TextBox2.Text += Button2.Text
        Else
            TextBox1.Text = Button2.Text
            TextBox2.Text += Button2.Text
            inputStatus = True
        End If
    End Sub


    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If inputStatus Then
            TextBox1.Text += Button3.Text
            TextBox2.Text += Button3.Text
        Else
            TextBox1.Text = Button3.Text
            TextBox2.Text += Button3.Text
            inputStatus = True
        End If
    End Sub


    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        If inputStatus Then
            TextBox1.Text += Button4.Text
            TextBox2.Text += Button4.Text
        Else
            TextBox1.Text = Button4.Text
            TextBox2.Text += Button4.Text
            inputStatus = True
        End If
    End Sub


    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        If inputStatus Then
            TextBox1.Text += Button5.Text
            TextBox2.Text += Button5.Text
        Else
            TextBox1.Text = Button5.Text
            TextBox2.Text += Button5.Text
            inputStatus = True
        End If
    End Sub


    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        If inputStatus Then
            TextBox1.Text += Button6.Text
            TextBox2.Text += Button6.Text
        Else
            TextBox1.Text = Button6.Text
            TextBox2.Text += Button6.Text
            inputStatus = True
        End If
    End Sub


    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        If inputStatus Then
            TextBox1.Text += Button7.Text
        Else
            TextBox1.Text = Button7.Text
            inputStatus = True
        End If
    End Sub


    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        If inputStatus Then
            TextBox1.Text += Button8.Text
        Else
            TextBox1.Text = Button8.Text
            inputStatus = True
        End If
    End Sub


    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        If inputStatus Then
            TextBox1.Text += Button9.Text
        Else
            TextBox1.Text = Button9.Text
            inputStatus = True
        End If
    End Sub


    Private Sub Button0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button0.Click
        'Check the input status
        If inputStatus Then 'If true
            'Now check to make sure our
            'input box has a value
            If TextBox1.Text.Length >= 1 Then
                'Add our zero
                TextBox1.Text += Button0.Text
                TextBox2.Text += Button0.Text
            End If
        End If
    End Sub


    Private Sub ButtonDot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonDot.Click
        'Check for input status (we want true)
        If inputStatus Then
            'Check if it already has a decimal (if it does then do nothing)
            If Not hasDecimal Then
                'Check to make sure the length is > than 1
                'Dont want user to add decimal as first character
                If TextBox1.Text.Length > 0 Then
                    'Make sure 0 isnt the first number
                    If Not TextBox1.Text = "0" Then
                        'It met all our requirements so add the zero
                        TextBox1.Text += ButtonDot.Text
                        TextBox2.Text += ButtonDot.Text
                        'Toggle the flag to true (only 1 decimal per calculation)
                        hasDecimal = True
                    End If
                Else
                    'Since the length isnt > 1
                    'make the text 0.
                    TextBox1.Text = "0."
                    TextBox2.Text = "0."
                End If
            End If
        End If
    End Sub


    Private Sub ButtonAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonAdd.Click
        'Make sure out input box has a value
        If TextBox1.Text.Length <> 0 Then
            'Check the value of our function flag
            If calcFunc = String.Empty Then 'Flag is empty
                'Assign the value in our input
                'box to our holder
                valHolder1 = CType(TextBox1.Text, Double)
                'Empty the input box
                TextBox1.Text = String.Empty


            Else 'Flag isnt empty
                'Call our calculate totals method


                CalculateTotals()
            End If
            TextBox2.Text += ButtonAdd.Text
            'Assign a value to our calc function flag
            calcFunc = "Add"
            'Toggle the decimal flag
            hasDecimal = False
        End If
    End Sub


    Private Sub ButtonSub_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSub.Click
        'Make sure out input box has a value
        If TextBox1.Text.Length <> 0 Then
            'Check the value of our function flag
            If calcFunc = String.Empty Then 'Flag is empty
                'Assign the value in our input
                'box to our holder
                valHolder1 = CType(TextBox1.Text, Double)
                'Empty the input box
                TextBox1.Text = String.Empty
                TextBox2.Text += ButtonSub.Text
            Else 'Flag isnt empty
                'Call our calculate totals method
                CalculateTotals()
            End If
            'Assign a value to our calc function flag
            calcFunc = "Subtract"
            'Toggle the decimal flag
            hasDecimal = False
        End If
    End Sub


    Private Sub ButtonMul_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonMul.Click
        'Make sure out input box has a value
        If TextBox1.Text.Length <> 0 Then
            'Check the value of our function flag
            If calcFunc = String.Empty Then 'Flag is empty
                'Assign the value in our input
                'box to our holder
                valHolder1 = CType(TextBox1.Text, Double)
                'Empty the input box
                TextBox1.Text = String.Empty
            Else 'Flag isnt empty
                'Call our calculate totals method
                CalculateTotals()
            End If
            'Assign a value to our calc function flag
            calcFunc = "Multiply"
            'Toggle the decimal flag
            hasDecimal = False
        End If
    End Sub




    Private Sub ButtonDiv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonDiv.Click
        'Make sure out input box has a value
        If TextBox1.Text.Length <> 0 Then
            'Check the value of our function flag
            If calcFunc = String.Empty Then 'Flag is empty
                'Assign the value in our input
                'box to our holder
                valHolder1 = CType(TextBox1.Text, Double)
                'Empty the input box
                TextBox1.Text = String.Empty
            Else 'Flag isnt empty
                'Call our calculate totals method
                CalculateTotals()
            End If
            'Assign a value to our calc function flag
            calcFunc = "Divide"
            'Toggle the decimal flag
            hasDecimal = False
        End If
    End Sub




    Private Sub ButtonRes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonRes.Click
        'Make sure theres a value in the input box
        'And that our temp value isnt 0
        If TextBox1.Text.Length <> 0 AndAlso valHolder1 <> 0 Then
            'Call the calculate totals method
            CalculateTotals()
            'Clear the calcFunction value
            calcFunc = String.Empty
            'Toggle the decimal flag
            hasDecimal = False
        End If
    End Sub


    'Private Sub ButtonBs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonBs.Click
    '    'Declare locals needed
    '    Dim str As String
    '    Dim loc As Integer
    '    'Make sure the text length is > 1
    '    If TextBox1.Text.Length > 0 Then
    '        'Get the next to last character
    '        str = TextBox1.Text.Chars(TextBox1.Text.Length - 1)
    '        str = TextBox2.Text.Chars(TextBox2.Text.Length - 1)
    '        'Check if its a decimal
    '        If str = "." Then
    '            'If it is toggle the hasDecimal flag
    '            hasDecimal = False
    '        End If
    '        'Get the length of the string
    '        loc = TextBox1.Text.Length
    '        loc = TextBox2.Text.Length
    '        'Remove the last character, incrementing by 1
    '        TextBox1.Text = TextBox1.Text.Remove(loc - 1, 1)
    '        TextBox2.Text = TextBox2.Text.Remove(loc - 1, 1)
    '    End If
    'End Sub


    Private Sub ButtonClr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonClr.Click
        'Empty the text in the input box
        TextBox1.Text = String.Empty
        TextBox2.Text = String.Empty
        'Clear out both temp values
        valHolder1 = 0
        valHolder2 = 0
        'Set the calc switch to empty
        calcFunc = String.Empty
        'Toggle the hasDecimal flag
        hasDecimal = False
    End Sub


    Private Sub CalculateTotals()
        valHolder2 = CType(TextBox1.Text, Double)
        Select Case calcFunc
            Case "Add"
                valHolder1 = valHolder1 + valHolder2
            Case "Subtract"
                valHolder1 = valHolder1 - valHolder2
            Case "Divide"
                valHolder1 = valHolder1 / valHolder2
            Case "Multiply"
                valHolder1 = valHolder1 * valHolder2
        End Select
        TextBox1.Text = CType(valHolder1, String)
        TextBox2.Text = CType(valHolder1, String)
        inputStatus = False
    End Sub




End Class
Thank YOU SO MUCH!!! :))
 
Top Bottom