Creating a Calculator | Rewritten in VB6

Some noticable differences. VB6 doesn’t have AppendText or += for Textboxes, so had to do Text1.text & “0”. There’s also no Return method for functions, so to return the result of our CalculateIt function we must set the function to equal the returned value. It’s the same thing in the end.

Dim oper, num1, num2 As String
Private Sub btn0_Click()
Text1.Text = Text1.Text & "0"
End Sub
Private Sub btn1_Click()
Text1.Text = Text1.Text & "1"
End Sub
Private Sub btn2_Click()
Text1.Text = Text1.Text & "2"
End Sub
Private Sub btn3_Click()
Text1.Text = Text1.Text & "3"
End Sub
Private Sub btn4_Click()
Text1.Text = Text1.Text & "4"
End Sub
Private Sub btn5_Click()
Text1.Text = Text1.Text & "5"
End Sub
Private Sub btn6_Click()
Text1.Text = Text1.Text & "6"
End Sub
Private Sub btn7_Click()
Text1.Text = Text1.Text & "7"
End Sub
Private Sub btn8_Click()
Text1.Text = Text1.Text & "8"
End Sub
Private Sub btn9_Click()
Text1.Text = Text1.Text & "9"
End Sub
Private Sub btnAdd_Click()
  If num1 = "" Then
            num1 = Text1.Text
            oper = "+"
            Text1.Text = ""
        Else
            num1 = CalculateIt(CDbl(num1), CDbl(Text1.Text), oper)
            oper = "+"
            Text1.Text = ""
        End If
End Sub
Private Sub btnDivide_Click()
  If num1 = "" Then
            num1 = Text1.Text
            oper = "+"
            Text1.Text = ""
        Else
            num1 = CalculateIt(CDbl(num1), CDbl(Text1.Text), oper)
            oper = "+"
            Text1.Text = ""
        End If
End Sub
Private Sub btnEquals_Click()
 If oper = "" Then
            MsgBox ("You must enter a second number")
            Exit Sub
        End If
        num2 = Text1.Text
        Text1.Text = CStr(CalculateIt(CDbl(num1), CDbl(num2), oper))
End Sub
Private Sub btnMinus_Click()
  If num1 = "" Then
            num1 = Text1.Text
            oper = "+"
            Text1.Text = ""
        Else
            num1 = CalculateIt(CDbl(num1), CDbl(Text1.Text), oper)
            oper = "+"
            Text1.Text = ""
        End If
End Sub
Private Sub btnMultiply_Click()
  If num1 = "" Then
            num1 = Text1.Text
            oper = "+"
            Text1.Text = ""
        Else
            num1 = CalculateIt(CDbl(num1), CDbl(Text1.Text), oper)
            oper = "+"
            Text1.Text = ""
        End If
End Sub
Private Sub btnClear_Click()
Text1.text = ""
oper = ""
num1 = ""
num2 = ""
End Sub

  Private Function CalculateIt(ByVal n1 As Double, n2 As Double, ByVal op As String) As Double
        Select Case op
            Case "+"
                CalculateIt = n1 + n2
            Case "-"
                CalculateIt = n1 - n2
            Case "*"
                CalculateIt = n1 * n2
            Case "/"
                CalculateIt = n1 / n2
        End Select
    End Function?

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.