Click here to Skip to main content
15,903,385 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Help pls~
i m currently making a simple calculator with 1 text box, +-*/, AC +/- and =....
but i can only work with 2 numbers.. it cant compute more than 2 numbers.. Eg: 2+2+3..
Here some of my coding..

VB
Dim x As String
Dim num1 As Double
Dim num2 As Double

Private Sub cmd8_Click()
    txtDisplay.Text = txtDisplay.Text & "8"
End Sub

Private Sub cmdAdd_Click()
    num1 = txtDisplay.Text
    txtDisplay.Text = ""
    x = "+"
End Sub ' <----- this was missing

Private Sub cmdEqual_Click()
    num2 = txtDisplay.Text
    If x = "+" Then
        txtDisplay.Text = num1 + num2
    ElseIf x = "-" Then
        txtDisplay.Text = num1 - num2
    ElseIf x = "*" Then
        txtDisplay.Text = num1 * num2
    ElseIf x = "/" Then
        If num2 = "0" Then
            txtDisplay.Text = num1 / num2
        End If
    End If   ' <--------- this was missing
End Sub 
Posted
Updated 6-May-11 2:42am
v2

1 solution

See my corrections to your code. Try using the debugger, and see what you come up with. The debugger is a free tool that's installed with Visual Studio.

BTW, it should go something like this:

VB
Private Sub cmdEqual_Click()
    num2 = txtDisplay.Text
    If x = "+" Then
        num1 += num2
    ElseIf x = "-" Then
        num1 -= num2
    ElseIf x = "*" Then
        num1 *= num2
    ElseIf x = "/" Then
        num1 /= Math.Max(1, num2)
    End If   ' <--------- this was missing
    txtDisplay.Text = Convert.ToString(num1)
End Sub
 
Share this answer
 
v2
Comments
Kent1010 6-May-11 8:58am    
the calculator can run.. but, for example, i click 1+2+3, the answer is 5.. the 1st number not included...
#realJSOP 6-May-11 9:35am    
That's because you're not setting the num1 variable to the value entered. Use the debugger, and you'll see this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900