VB Tutor VB.NET 2022 Tutorial VB2019 Tutorial VB6 Tutorial VB Sample Code About Us
Visual Basic Sample Code

Scientific Calculator

Interactive implementation with VB6 and VB.NET code examples


This scientific calculator resembles a typical scientific calculator, albeit a simpler version. We've included trigonometric functions (Sin, Cos, Tan) and logarithmic functions (Log, Ln) to demonstrate essential mathematical operations in programming.

The reason for creating a simpler version of the calculator is to help users learn programming concepts gradually without overwhelming complexity. This approach is especially valuable for those learning to program in Visual Basic.

Trigonometric Functions in Visual Basic

  • Sin computes the sine of an angle in radians
  • Cos computes the cosine of an angle in radians
  • Tan computes the tangent of an angle in radians
  • Atn computes the arc tangent of an angle in radians

Log computes the base-10 logarithm, while Ln computes the natural logarithm (base e).

Tip: Angles in degrees must be converted to radians before using trigonometric functions. π radians = 180°, so multiply by (π/180) to convert.

0

Implementation Code

Below you'll find the implementation code for the scientific calculator in both VB6 and VB.NET.

VB6 Code
VB.NET Code

VB6 Implementation

Here's how to implement the scientific calculator in Visual Basic 6:

Private displayValue As Double

' Convert degrees to radians
Private Function ToRadians(degrees As Double) As Double
    ToRadians = degrees * (4 * Atn(1)) / 180
End Function

' Sine function
Private Sub CmdSin_Click()
    Dim sinValue As Double
    sinValue = Round(Sin(ToRadians(displayValue)), 4)
    panel.Caption = sinValue
End Sub

' Cosine function
Private Sub CmdCos_Click()
    Dim cosValue As Double
    cosValue = Round(Cos(ToRadians(displayValue)), 4)
    panel.Caption = cosValue
End Sub

' Tangent function
Private Sub CmdTan_Click()
    Dim tanValue As Double
    tanValue = Round(Tan(ToRadians(displayValue)), 4)
    panel.Caption = tanValue
End Sub

' Natural logarithm
Private Sub CmdLn_Click()
    panel.Caption = Log(displayValue)
End Sub

' Base-10 logarithm
Private Sub CmdLog_Click()
    panel.Caption = Log(displayValue) / Log(10)
End Sub

' Clear display
Private Sub CmdClear_Click()
    displayValue = 0
    panel.Caption = "0"
End Sub

' Append digit to display
Private Sub AppendDigit(digit As String)
    If panel.Caption = "0" Then
        panel.Caption = digit
    Else
        panel.Caption = panel.Caption & digit
    End If
    displayValue = Val(panel.Caption)
End Sub

VB.NET Implementation

Here's the modern implementation in VB.NET with enhanced features:

Public Class ScientificCalculator
    Private currentValue As Double = 0
    Private currentInput As String = "0"
    Private lastOperation As String = ""
    Private memory As Double = 0

    ' Convert degrees to radians
    Private Function ToRadians(degrees As Double) As Double
        Return degrees * (Math.PI / 180)
    End Function

    ' Update display
    Private Sub UpdateDisplay()
        txtDisplay.Text = currentInput
    End Sub

    ' Digit button click handler
    Private Sub Digit_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 = DirectCast(sender, Button)
        If currentInput = "0" Then
            currentInput = btn.Text
        Else
            currentInput &= btn.Text
        End If
        UpdateDisplay()
    End Sub

    ' Scientific functions
    Private Sub ScientificFunction(sender As Object, e As EventArgs) Handles _
        btnSin.Click, btnCos.Click, btnTan.Click, btnLog.Click, btnLn.Click, btnPi.Click

        Dim btn = DirectCast(sender, Button)
        Dim value As Double = If(currentInput = "", 0, Double.Parse(currentInput))
        Dim result As Double = 0

        Try
            Select Case btn.Name
                Case "btnSin"
                    result = Math.Sin(ToRadians(value))
                Case "btnCos"
                    result = Math.Cos(ToRadians(value))
                Case "btnTan"
                    result = Math.Tan(ToRadians(value))
                Case "btnLog"
                    result = Math.Log10(value)
                Case "btnLn"
                    result = Math.Log(value)
                Case "btnPi"
                    result = Math.PI
            End Select

            currentInput = result.ToString()
            UpdateDisplay()
        Catch ex As Exception
            currentInput = "Error"
            UpdateDisplay()
        End Try
    End Sub

    ' Clear button
    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        currentInput = "0"
        currentValue = 0
        lastOperation = ""
        UpdateDisplay()
    End Sub

    ' Equals button
    Private Sub btnEquals_Click(sender As Object, e As EventArgs) Handles btnEquals.Click
        If Not String.IsNullOrEmpty(lastOperation) Then
            Dim secondValue As Double = Double.Parse(currentInput)
            Try
                Select Case lastOperation
                    Case "+"
                        currentValue += secondValue
                    Case "-"
                        currentValue -= secondValue
                    Case "×"
                        currentValue *= secondValue
                    Case "÷"
                        If secondValue <> 0 Then
                            currentValue /= secondValue
                        Else
                            Throw New DivideByZeroException()
                        End If
                End Select
                currentInput = currentValue.ToString()
                UpdateDisplay()
            Catch ex As Exception
                currentInput = "Error"
                UpdateDisplay()
            Finally
                lastOperation = ""
            End Try
        End If
    End Sub

Key Improvements in VB.NET Version

  • Uses .NET's Math class for more accurate calculations
  • Includes exception handling for mathematical errors
  • Implements memory functions (MC, MR, M+, M-)
  • Better event handling with button groups
  • Supports continuous calculations
Visual Basic Programming Book

VISUAL BASIC PROGRAMMING WITH CODE EXAMPLES

A Practical Guide authored by Dr. Liew Voon Kiong for Beginners and Intermediate Developers to Master Visual Basic programming—From VB6 to VB.NET

Unlock the power of Visual Basic with this comprehensive, hands-on guide that bridges the gap between classic VB6 and modern VB.NET programming. Whether you're maintaining legacy systems or developing cutting-edge Windows applications, this book gives you the knowledge, tools, and inspiration to succeed.

Visual Basic Programming with Code Examples offers a unique dual-format approach, showcasing sample codes in both Visual Basic 6 (VB6) and VB.NET. This side-by-side presentation helps you understand the evolution of Visual Basic and empowers you to work confidently across both environments.

What You'll Learn:

  • Core Concepts Made Easy: Explore data types, control structures, file handling, procedures, user interface design, and more.
  • Hands-On Application Building: Design real-world applications, including financial calculators, educational tools, games, multimedia apps, and database systems.
  • Practical Code Examples: Study and customize fully explained programs that illustrate key programming techniques.
  • Dual-Code Format: Learn to translate and adapt code between VB6 and VB.NET seamlessly.
Buy Now on Amazon Kindle Edition