vurquee
vurquee

Reputation: 491

VB6 Calculator: Show operator sign on the screen

I just started learning VB6 on my own. I have created a simple calculator and I would like it to display the "operator" on the screen.

For example, if I press "1", followed by "plus sign", then finally "8", I would like the calculator to show "1 + 8". And when the "equal" sign is pressed, the calculator should show "1 + 8 = 9".

Below is a very noob code I made:

    Dim formula As String
    Dim itemOne As Integer
    Dim itemTwo As Integer

    Private Sub btn1_Click()
    txtboxScreen.Text = txtboxScreen.Text & 1
    End Sub

    Private Sub btn2_Click()
    txtboxScreen.Text = txtboxScreen.Text & 2
    End Sub

    Private Sub btn3_Click()
    txtboxScreen.Text = txtboxScreen.Text & 3
    End Sub

    Private Sub btn4_Click()
    txtboxScreen.Text = txtboxScreen.Text & 4
    End Sub

    Private Sub btn5_Click()
    txtboxScreen.Text = txtboxScreen.Text & 5
    End Sub

    Private Sub btn6_Click()
    txtboxScreen.Text = txtboxScreen.Text & 6
    End Sub

    Private Sub btn7_Click()
    txtboxScreen.Text = txtboxScreen.Text & 7
    End Sub

    Private Sub btn8_Click()
    txtboxScreen.Text = txtboxScreen.Text & 8
    End Sub

    Private Sub btn9_Click()
    txtboxScreen.Text = txtboxScreen.Text & 9
    End Sub

    Private Sub btnDivide_Click()
    itemOne = txtboxScreen.Text
    txtboxScreen.Text = ""
    formula = "/"
    End Sub

    Private Sub btnEqual_Click()
    itemTwo = txtboxScreen.Text
    If formula = "+" Then
    txtboxScreen.Text = itemOne + itemTwo
    ElseIf formula = "-" Then
    txtboxScreen.Text = itemOne - itemTwo
    ElseIf formula = "*" Then
    txtboxScreen.Text = itemOne * itemTwo
    ElseIf formula = "/" Then
    txtboxScreen.Text = itemOne / itemTwo
    End If
    End Sub

    Private Sub btnMinus_Click()
    itemOne = txtboxScreen.Text
    txtboxScreen.Text = ""
    formula = "-"
    End Sub

    Private Sub btnPlus_Click()
    itemOne = txtboxScreen.Text
    txtboxScreen.Text = ""
    formula = "+"
    End Sub

    Private Sub btnTimes_Click()
    itemOne = txtboxScreen.Text
    txtboxScreen.Text = ""
    formula = "*"
    End Sub

    Private Sub btnZero_Click()
    txtboxScreen.Text = txtboxScreen.Text & 0
    End Sub

Upvotes: 0

Views: 2294

Answers (3)

Mark Kram
Mark Kram

Reputation: 5842

You may want to think about using a control array for you number buttons. This will vastly simplify your code in this instance and especially for more complex projects:

    Private formula As String
    Private itemOne As Integer
    Private itemTwo As Integer

    Private Sub btnNumbers_Click(Index As Integer)
          txtboxScreen.Text = txtboxScreen.Text & Index
    End Sub

''Remainder of your code goes here

Also, when you are declaring variables in the Declaration section of the form you should use Private instead of Dim.

Upvotes: 3

Hossein
Hossein

Reputation: 4137

You should save your first number, second number and operator (you called it "formula") separately and handle setting the text of the text box separately. Here is one way to do it:

Dim formula As String
Dim itemOne As String 'This time this is string
Dim itemTwo As String 'This time this is string

Dim currentItem As String 'Will hold the current number being entered
Dim Result As String 'This is string, too.

All your buttons will have code like:

Private Sub btn1_Click()
    currentItem = currentItem & "1"
    UpdateText()
End Sub

The operator buttons:

Private Sub btnPlus_Click()
    itemOne = currentItem
    formula = "+"
    UpdateText()
End Sub

And the Equals button:

Private Sub btnEqual_Click()
    itemTwo = currentItem
    If formula = "+" Then
        'Str is optional, but Val's are necessary since
        'itemOne and itemTwo are strings.
        Result = Str( Val(itemOne) + Val(itemTwo) )
    ElseIf ...
        .
        .
        .
    End If
    UpdateText()
End Sub

Well, noticed the call to UpdateText() at the end of every subprocedure? Here it is:

Private Sub UpdateText()
    txtboxScreen.Text = itemOne & formula & itemTwo
    'If result is not empty, we will add the '=' part too
    If Result <> "" Then
        txtboxScreen.Text = txtboxScreen.Text & "=" & Result
    End If
End Sub

You might also be interested in an AC/ON key which sets all the variables to "".

The method was not so neat, but it's the best thing you can do without an expression evaluator. An expression evaluator can calculate the entire formula as it is. Eval is such a function and you can find some implementations of it on the net.

Upvotes: 1

YanovskiShai
YanovskiShai

Reputation: 11

I think you would like to concatenate the operator sign of the pressed button with the value you had in the textbox on button_click event.

something like:

Private Sub btnPlus_Click()
    txtboxScreen.Text = txtboxScreen.Text & " + "
End Sub

and on equal button, you'd like to evaluate the expression

Private Sub btnEqual_Click()
    txtboxScreen.Text = txtboxScreen.Text & " = " & Eval(txtboxScreen.Text)
End Sub

evaluating using Eval() is not a robust solution, but it's a simple way to achive that functionality.

Upvotes: 1

Related Questions