1. Home
  2. Computing & Technology
  3. Visual Basic

The New Calculator Program
Source Code

By , About.com Guide

Public Class NewCalc
  Inherits System.Windows.Forms.Form
  Private Accum As String
  Private NumQueue As New Queue
  Private OpQueue As New Queue
#Region " Windows Form Designer generated code "

  Private Sub op_Click( _
  ByVal sender As System.Object, _
  ByVal e As System.EventArgs) _
  Handles opplus.Click, opminus.Click, _
    opmult.Click, opdivide.Click
    OpQueue.Enqueue(sender.tag)
    NumQueue.Enqueue(Accum)
    Accum = ""
  End Sub

  Private Sub opequals_Click( _
    ByVal sender As Object, _
    ByVal e As System.EventArgs) _
    Handles opequals.Click
    Dim CurrentOp As String
    Dim Num1, Num2 As Double
    NumQueue.Enqueue(Accum)
    Try
      Num1 = CDbl(NumQueue.Dequeue)
      Num2 = CDbl(NumQueue.Dequeue)
      Do
        CurrentOp = OpQueue.Dequeue
        Select Case CurrentOp
          Case "Divide"
            If Num2 <> 0 Then
              Num1 /= CDbl(Num2)
            Else
              CalcPadDisplay.Text = _
              "Divide By Zero"
              Exit Sub
            End If
          Case "Multiply"
            Num1 *= CDbl(Num2)
          Case "Add"
            Num1 += CDbl(Num2)
          Case "Subtract"
            Num1 -= CDbl(Num2)
        End Select
        If NumQueue.Count = 0 Then Exit Do
        Num2 = NumQueue.Dequeue
      Loop
      CalcPadDisplay.Text = CStr(Num1)
      Accum = ""
    Catch
      OpQueue.Clear()
      NumQueue.Clear()
      Accum = ""
      CalcPadDisplay.Text = "0"
      Exit Sub
    End Try
  End Sub

  Private Sub keyClick( _
    ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles key1.Click, key2.Click, key3.Click, _
        key4.Click, key5.Click, key6.Click, _
        key7.Click, key8.Click, key9.Click, _
        key0.Click, keydecimal.Click
    If InStr(Accum, ".") <> 0 Then
      Exit Sub
    End If
    If Len(Accum) = 0 Then
      ' First keypress
      Accum = sender.tag
    Else
      ' Second or later keypress
      Accum &= sender.tag
    End If
    CalcPadDisplay.Text = Accum
  End Sub

  Private Sub opclear_Click( _
  ByVal sender As Object, _
  ByVal e As System.EventArgs) _
  Handles opclear.Click, opclearentry.Click
    CalcPadDisplay.Text = "0"
    Accum = ""
    If sender.tag = "Clear" Then
      OpQueue.Clear()
      NumQueue.Clear()
    End If
  End Sub
End Class

Thanks to an About Visual Basic reader for correcting a bug that was causing continued calculations, such as ((12 * 3) - 2), from being calculated correctly.

Explore Visual Basic
By Category
About.com Special Features

The Best Web Trends of the Decade

A look back at the best innovations, ideas and technologies over the last 10 years, More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Using VB.NET
  5. Queues and Stacks - Another great tool in VB.NET

©2010 About.com, a part of The New York Times Company.

All rights reserved.