Learn to create a calculator in VB6 and VB.NET with interactive examples
This tutorial demonstrates how to create a calculator application in both Visual Basic 6 and VB.NET. The calculator includes standard arithmetic operations, memory functions, and a clear display. The interface consists of number buttons, operator buttons, and additional functional buttons.
To design the interface in VB6, you need to insert 25 command buttons and one label that functions as the display panel. The number buttons from 1 to 9 are grouped together as a control array named ButtonNum, while 0 is a standalone command named Bzero.
Try out the calculator functionality directly in your browser. This interactive demo mimics the behavior of the VB6 calculator described in this tutorial.
The most important procedure in the program is to display the number on the panel. To display the number, click on any number button and enter the following code:
Private Sub ButtonNum_Click(Index As Integer)
If Num_of_digit > 0 Then
If Num_of_digit < 30 Then
panel.Caption = panel.Caption + Right$(Str(Index), 1)
Num_of_digit = Num_of_digit + 1
End If
Else
panel.Caption = Right$(Str(Index), 1)
Num_of_digit = 1
End If
CheckValue
End Sub
The Num_of_digit variable checks the number of digits that appear on the display panel. The procedure ensures that if the number of digits is more than one, the preceding digit will be pushed to the left and the succeeding digit will remain on the right.
We introduce variables a,b,c,d,e,f to accept the value of the first number entered by the user and use the variable key to determine what arithmetic operation to perform when the Equal button is pressed.
Private Sub Operator_Click(Index As Integer)
CheckValue
If Index = 11 Then
a = displayValue
key = 1
ElseIf Index = 12 Then
b = displayValue
key = 2
ElseIf Index = 13 Then
c = displayValue
key = 3
ElseIf Index = 14 Then
d = displayValue
key = 4
ElseIf Index = 15 Then
f = displayValue
key = 5
End If
Num_of_digit = 0
newNumber = True
End Sub
Private Sub Equal_Click()
CheckValue
If newNumber = True Then
If key = 1 Then
e = displayValue + a
ElseIf key = 2 Then
e = b - displayValue
ElseIf key = 3 Then
e = displayValue * c
ElseIf key = 5 Then
e = (f * displayValue) / 100
ElseIf key = 4 And displayValue <> 0 Then
e = d / displayValue
Else
GoTo error
End If
If Abs(e) < 1 Then
panel.Caption = Format(e, "General Number")
Else
panel.Caption = Str(e)
End If
Else
panel.Caption = displayValue
End If
GoTo finish
error:
panel.Caption = "E"
finish:
Num_of_digit = 0
newNumber = False
End Sub
The VB6 Calculator Interface
Here's the equivalent calculator implementation in VB.NET. The code structure is similar but takes advantage of VB.NET's modern features.
Public Class CalculatorForm
Private firstOperand As Double
Private secondOperand As Double
Private [operator] As String
Private newInput As Boolean = True
Private Sub NumberButton_Click(sender As Object, e As EventArgs) _
Handles btn0.Click, btn1.Click, btn2.Click, btn3.Click,
btn4.Click, btn5.Click, btn6.Click, btn7.Click,
btn8.Click, btn9.Click
Dim btn As Button = CType(sender, Button)
If newInput Then
txtDisplay.Text = btn.Text
newInput = False
Else
txtDisplay.Text += btn.Text
End If
End Sub
Private Sub OperatorButton_Click(sender As Object, e As EventArgs) _
Handles btnAdd.Click, btnSubtract.Click, btnMultiply.Click,
btnDivide.Click, btnPercent.Click
Dim btn As Button = CType(sender, Button)
firstOperand = Double.Parse(txtDisplay.Text)
[operator] = btn.Text
newInput = True
End Sub
Private Sub btnEquals_Click(sender As Object, e As EventArgs) Handles btnEquals.Click
If Not newInput Then
secondOperand = Double.Parse(txtDisplay.Text)
Select Case [operator]
Case "+"
txtDisplay.Text = (firstOperand + secondOperand).ToString()
Case "-"
txtDisplay.Text = (firstOperand - secondOperand).ToString()
Case "×"
txtDisplay.Text = (firstOperand * secondOperand).ToString()
Case "÷"
If secondOperand <> 0 Then
txtDisplay.Text = (firstOperand / secondOperand).ToString()
Else
txtDisplay.Text = "Error"
End If
Case "%"
txtDisplay.Text = (firstOperand * secondOperand / 100).ToString()
End Select
newInput = True
End If
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
txtDisplay.Text = "0"
firstOperand = 0
secondOperand = 0
[operator] = ""
newInput = True
End Sub
Private Sub btnDecimal_Click(sender As Object, e As EventArgs) Handles btnDecimal.Click
If Not txtDisplay.Text.Contains(".") Then
txtDisplay.Text += "."
End If
End Sub
End Class
The VB.NET implementation is more maintainable and follows modern programming practices while maintaining the core calculator functionality.
Try to extend the calculator with these features:
Advanced Challenge: Implement the calculator using a stack-based approach to handle complex expressions with multiple operations and parentheses.
This calculator sample is part of the Visual Basic Sample Code Collection by Dr. Liew Voon Kiong. The examples demonstrate fundamental programming concepts in both VB6 and VB.NET, showing the evolution of the Visual Basic language.
For more comprehensive examples and tutorials, see our book Visual Basic Programming with Code Examples which includes 48 practical programs with both VB6 and VB.NET implementations.