VB Tutor VB2022 VB2019 VB6 VB Sample Code About Us
Visual Basic Sample Code

Interactive Calculator Demo

VB6 and VB.NET implementations with live JavaScript calculator


This calculator demo showcases how to create a simple calculator application in both Visual Basic 6 and VB.NET. The calculator performs basic arithmetic operations including addition, subtraction, multiplication, and division.

Below you'll find:

  • A live interactive calculator you can use right now
  • Visual Basic 6 implementation code
  • Visual Basic .NET implementation code
  • Explanation of key concepts

Live Calculator Demo

Try the calculator below. This JavaScript implementation mimics the functionality of our VB examples.

0

Visual Basic Implementation

VB6 Code
VB.NET Code

This VB6 implementation uses a form with text boxes for input, a combo box for operator selection, and a command button to perform the calculation.

Private Sub Combo1_Change()
    Call CalculateResult
End Sub

Private Sub Command1_Click()
    Call CalculateResult
End Sub

Private Sub CalculateResult()
    Dim num1 As Double
    Dim num2 As Double
    Dim result As Double
    
    ' Get values from text boxes
    If IsNumeric(Text1.Text) Then
        num1 = CDbl(Text1.Text)
    Else
        MsgBox "Please enter a valid number in the first field", vbExclamation
        Exit Sub
    End If
    
    If IsNumeric(Text2.Text) Then
        num2 = CDbl(Text2.Text)
    Else
        MsgBox "Please enter a valid number in the second field", vbExclamation
        Exit Sub
    End If
    
    ' Perform calculation based on selected operator
    Select Case Combo1.ListIndex
        Case 0 ' Addition
            result = num1 + num2
        Case 1 ' Subtraction
            result = num1 - num2
        Case 2 ' Multiplication
            result = num1 * num2
        Case 3 ' Division
            If num2 = 0 Then
                MsgBox "Cannot divide by zero", vbExclamation
                Exit Sub
            End If
            result = num1 / num2
    End Select
    
    ' Display result
    Label1.Caption = Format(result, "0.00")
End Sub

Private Sub Command2_Click()
    Unload Me
End Sub

Private Sub Form_Load()
    ' Initialize combo box with operators
    Combo1.AddItem "+"
    Combo1.AddItem "-"
    Combo1.AddItem "×"
    Combo1.AddItem "÷"
    Combo1.ListIndex = 0 ' Select first item
End Sub

This VB.NET implementation uses Windows Forms with similar controls but includes better error handling and modern features.

Public Class CalculatorForm
    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        Call CalculateResult()
    End Sub

    Private Sub CalculateButton_Click(sender As Object, e As EventArgs) Handles CalculateButton.Click
        Call CalculateResult()
    End Sub

    Private Sub CalculateResult()
        Dim num1, num2, result As Double
        
        ' Validate and parse first number
        If Not Double.TryParse(TextBox1.Text, num1) Then
            MessageBox.Show("Please enter a valid number in the first field", "Input Error", 
                            MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Return
        End If
        
        ' Validate and parse second number
        If Not Double.TryParse(TextBox2.Text, num2) Then
            MessageBox.Show("Please enter a valid number in the second field", "Input Error", 
                            MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Return
        End If
        
        ' Perform calculation based on selected operator
        Select Case ComboBox1.SelectedIndex
            Case 0 ' Addition
                result = num1 + num2
            Case 1 ' Subtraction
                result = num1 - num2
            Case 2 ' Multiplication
                result = num1 * num2
            Case 3 ' Division
                If num2 = 0 Then
                    MessageBox.Show("Cannot divide by zero", "Math Error", 
                                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                    Return
                End If
                result = num1 / num2
        End Select
        
        ' Display formatted result
        ResultLabel.Text = result.ToString("N2")
    End Sub

    Private Sub ExitButton_Click(sender As Object, e As EventArgs) Handles ExitButton.Click
        Me.Close()
    End Sub

    Private Sub CalculatorForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' Initialize combo box with operators
        ComboBox1.Items.AddRange(New Object() {"+", "-", "×", "÷"})
        ComboBox1.SelectedIndex = 0
    End Sub
End Class

Implementation Details

Both VB6 and VB.NET implementations follow similar logic but with important differences in syntax and error handling:

VB6 Implementation

  • Form Controls: Uses TextBox, ComboBox, Label, and CommandButton controls
  • Event Handling: Uses event procedures like Combo1_Change and Command1_Click
  • Input Validation: Uses IsNumeric() to check for valid numbers
  • Error Handling: Displays message boxes for errors
  • Division by Zero: Explicit check to prevent calculation errors

VB.NET Implementation

  • Modern Controls: Similar controls but with improved properties and events
  • Better Validation: Uses Double.TryParse() for more robust number parsing
  • Structured Error Handling: Uses MessageBox for user-friendly error messages
  • Resource Management: Automatic resource cleanup with garbage collection
  • Event Handling: Uses Handles keyword for cleaner event attachment

Tip: When migrating from VB6 to VB.NET, pay special attention to:

  • Data type changes (e.g., Integer is now 32-bit instead of 16-bit)
  • Control property name changes
  • Improved error handling with Try/Catch blocks
  • Object-oriented features in VB.NET