Visual Basic 6.0 Source Code for Simple Calculator
This guide provides the complete Visual Basic 6.0 source code for a simple calculator application. The code includes all necessary components for basic arithmetic operations, user interface elements, and event handling. Whether you're learning VB6 or need a reference for your own projects, this implementation demonstrates fundamental programming concepts in a practical way.
Introduction
Visual Basic 6.0 (VB6) remains an important legacy language for many developers, particularly in enterprise environments where it was widely used. While newer languages have largely replaced VB6, understanding its fundamentals can still be valuable for maintaining older systems or learning the principles of event-driven programming.
This simple calculator demonstrates core VB6 concepts including form design, event handling, and basic arithmetic operations. The implementation is straightforward enough to serve as a learning tool while being functional enough for practical use.
Visual Basic 6.0 Source Code
The following is the complete source code for a simple calculator in Visual Basic 6.0. This implementation includes all necessary components for basic arithmetic operations.
Calculator Form Code
' Calculator Form Code
' frmCalculator.frm
VERSION 5.00
Begin VB.Form frmCalculator
Caption = "Simple Calculator"
ClientHeight = 3000
ClientLeft = 45
ClientTop = 315
ClientWidth = 4500
LinkTopic = "Form1"
ScaleHeight = 3000
ScaleWidth = 4500
StartUpPosition = 1 'Windows Default
Begin VB.TextBox txtDisplay
Height = 315
Left = 480
TabIndex = 0
Text = "0"
Top = 360
Width = 3540
End
Begin VB.CommandButton cmd1
Caption = "1"
Height = 315
Left = 480
TabIndex = 1
Top = 720
Width = 855
End
Begin VB.CommandButton cmd2
Caption = "2"
Height = 315
Left = 1365
TabIndex = 2
Top = 720
Width = 855
End
Begin VB.CommandButton cmd3
Caption = "3"
Height = 315
Left = 2250
TabIndex = 3
Top = 720
Width = 855
End
Begin VB.CommandButton cmdPlus
Caption = "+"
Height = 315
Left = 3135
TabIndex = 4
Top = 720
Width = 855
End
Begin VB.CommandButton cmd4
Caption = "4"
Height = 315
Left = 480
TabIndex = 5
Top = 1080
Width = 855
End
Begin VB.CommandButton cmd5
Caption = "5"
Height = 315
Left = 1365
TabIndex = 6
Top = 1080
Width = 855
End
Begin VB.CommandButton cmd6
Caption = "6"
Height = 315
Left = 2250
TabIndex = 7
Top = 1080
Width = 855
End
Begin VB.CommandButton cmdMinus
Caption = "-"
Height = 315
Left = 3135
TabIndex = 8
Top = 1080
Width = 855
End
Begin VB.CommandButton cmd7
Caption = "7"
Height = 315
Left = 480
TabIndex = 9
Top = 1440
Width = 855
End
Begin VB.CommandButton cmd8
Caption = "8"
Height = 315
Left = 1365
TabIndex = 10
Top = 1440
Width = 855
End
Begin VB.CommandButton cmd9
Caption = "9"
Height = 315
Left = 2250
TabIndex = 11
Top = 1440
Width = 855
End
Begin VB.CommandButton cmdMultiply
Caption = "*"
Height = 315
Left = 3135
TabIndex = 12
Top = 1440
Width = 855
End
Begin VB.CommandButton cmd0
Caption = "0"
Height = 315
Left = 480
TabIndex = 13
Top = 1800
Width = 855
End
Begin VB.CommandButton cmdDecimal
Caption = "."
Height = 315
Left = 1365
TabIndex = 14
Top = 1800
Width = 855
End
Begin VB.CommandButton cmdEquals
Caption = "="
Height = 315
Left = 2250
TabIndex = 15
Top = 1800
Width = 855
End
Begin VB.CommandButton cmdDivide
Caption = "/"
Height = 315
Left = 3135
TabIndex = 16
Top = 1800
Width = 855
End
Begin VB.CommandButton cmdClear
Caption = "C"
Height = 315
Left = 480
TabIndex = 17
Top = 2160
Width = 3540
End
End
Attribute VB_Name = "frmCalculator"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Calculator Module Code
' Calculator Module Code
' frmCalculator.bas
Option Explicit
Private currentValue As Double
Private previousValue As Double
Private currentOperation As String
Private isNewNumber As Boolean
Private Sub cmd0_Click()
AddDigit "0"
End Sub
Private Sub cmd1_Click()
AddDigit "1"
End Sub
Private Sub cmd2_Click()
AddDigit "2"
End Sub
Private Sub cmd3_Click()
AddDigit "3"
End Sub
Private Sub cmd4_Click()
AddDigit "4"
End Sub
Private Sub cmd5_Click()
AddDigit "5"
End Sub
Private Sub cmd6_Click()
AddDigit "6"
End Sub
Private Sub cmd7_Click()
AddDigit "7"
End Sub
Private Sub cmd8_Click()
AddDigit "8"
End Sub
Private Sub cmd9_Click()
AddDigit "9"
End Sub
Private Sub cmdDecimal_Click()
If isNewNumber Then
txtDisplay.Text = "0."
isNewNumber = False
ElseIf InStr(txtDisplay.Text, ".") = 0 Then
txtDisplay.Text = txtDisplay.Text & "."
End If
End Sub
Private Sub cmdClear_Click()
currentValue = 0
previousValue = 0
currentOperation = ""
txtDisplay.Text = "0"
isNewNumber = True
End Sub
Private Sub cmdPlus_Click()
SetOperation "+"
End Sub
Private Sub cmdMinus_Click()
SetOperation "-"
End Sub
Private Sub cmdMultiply_Click()
SetOperation "*"
End Sub
Private Sub cmdDivide_Click()
SetOperation "/"
End Sub
Private Sub cmdEquals_Click()
If currentOperation <> "" Then
previousValue = Val(txtDisplay.Text)
Select Case currentOperation
Case "+"
currentValue = currentValue + previousValue
Case "-"
currentValue = currentValue - previousValue
Case "*"
currentValue = currentValue * previousValue
Case "/"
If previousValue <> 0 Then
currentValue = currentValue / previousValue
Else
MsgBox "Cannot divide by zero", vbExclamation
Exit Sub
End If
End Select
txtDisplay.Text = CStr(currentValue)
currentOperation = ""
isNewNumber = True
End If
End Sub
Private Sub AddDigit(digit As String)
If isNewNumber Then
txtDisplay.Text = digit
isNewNumber = False
Else
If txtDisplay.Text = "0" Then
txtDisplay.Text = digit
Else
txtDisplay.Text = txtDisplay.Text & digit
End If
End If
End Sub
Private Sub SetOperation(operation As String)
If currentOperation <> "" Then
cmdEquals_Click
End If
currentValue = Val(txtDisplay.Text)
currentOperation = operation
isNewNumber = True
End Sub
How the Calculator Works
The calculator implements basic arithmetic operations using event-driven programming. Here's how it works:
User Interface
The form contains:
- A display text box (txtDisplay) to show the current number and results
- Number buttons (0-9) for input
- Operation buttons (+, -, *, /) for calculations
- Decimal point button for floating-point numbers
- Equals button to compute the result
- Clear button to reset the calculator
Event Handling
Each button has a corresponding Click event handler that performs specific actions:
- Number buttons append digits to the current number
- Operation buttons store the current number and prepare for the next operation
- The equals button performs the calculation and displays the result
- The clear button resets all values and the display
Calculation Logic
The module maintains several private variables:
- currentValue - stores the first operand or accumulated result
- previousValue - stores the second operand
- currentOperation - stores the pending operation (+, -, *, /)
- isNewNumber - flag to indicate if a new number should start
The calculation follows this sequence:
- User enters first number and selects an operation
- The calculator stores the number and operation
- User enters second number and clicks equals
- The calculator performs the operation and displays the result
Note: This implementation handles basic arithmetic but doesn't include error checking for invalid operations or overflow conditions.
Example Calculation
Let's walk through a sample calculation: 25 + 17 = 42
- User clicks "2" then "5" - display shows "25"
- User clicks "+" - calculator stores 25 as currentValue and "+" as currentOperation
- User clicks "1" then "7" - display shows "17"
- User clicks "=" - calculator computes 25 + 17 = 42 and displays "42"
This demonstrates the basic flow of the calculator's operation handling.
Frequently Asked Questions
- Can I modify this calculator to include more operations?
- Yes, you can add more operations by creating additional buttons and corresponding event handlers. The current structure makes it easy to extend with new functions.
- How do I change the appearance of the calculator?
- You can modify the form properties in the VB6 IDE to change colors, sizes, and positions of controls. The current design uses standard VB6 controls.
- What happens if I divide by zero?
- The calculator includes a check for division by zero and displays an error message. This prevents the application from crashing.
- Can I save the calculator as an executable?
- Yes, you can compile the project in VB6 to create an executable file (.exe) that can be run on any Windows system with the VB6 runtime installed.
- Is this code compatible with modern Windows versions?
- The code should work on modern Windows versions as long as the VB6 runtime is installed. However, for new projects, consider using more modern development tools.