1. Home
  2. Computing & Technology
  3. Visual Basic
Public Class CalcPad
    Inherits System.Windows.Forms.UserControl

#Region " Windows Form Designer generated code "

    Private Accum As Double
    Private PendingOp As String
    Private PendingOpFlag As Boolean

    Property DisplayValue() As String
        Get
            Return CalcPadDisplay.Text
        End Get
        Set(ByVal Value As String)
        End Set
    End Property

    Private Sub CalcPad_Load( _
        ByVal sender As Object, _
        ByVal e As System.EventArgs) _
        Handles MyBase.Load
        PendingOp = "Null"
        PendingOpFlag = False
        Accum = 0
    End Sub

    Private Sub op( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles opplus.Click, opminus.Click, _
            opdivide.Click, opmult.Click
        Dim DisplayValue As Double
        DisplayValue = CDec(CalcPadDisplay.Text)
        If Accum = 0 Then
            Accum = DisplayValue
        Else
            Accum = AccumCalc(PendingOp)
            CalcPadDisplay.Text = CStr(Accum)
        End If
        PendingOp = sender.tag
        PendingOpFlag = True
    End Sub

    Private Sub opequals_Click( _
        ByVal sender As Object, _
        ByVal e As System.EventArgs) _
        Handles opequals.Click
        CalcPadDisplay.BackColor = Color.White
        If PendingOp <> "Null" Then
            Accum = AccumCalc(PendingOp)
            CalcPadDisplay.Text = Accum
            PendingOp = "Null"
            PendingOpFlag = True
            Accum = 0
        End If
    End Sub

    Private Sub opclear_Click( _
        ByVal sender As Object, _
        ByVal e As System.EventArgs) _
        Handles opclear.Click
        CalcPadDisplay.BackColor = Color.White
        CalcPadDisplay.Text = "0"
        PendingOp = "Null"
        Accum = 0
    End Sub

    Private Sub opclearentry_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles opclearentry.Click
        CalcPadDisplay.BackColor = Color.White
        CalcPadDisplay.Text = "0"
    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
        CalcPadDisplay.BackColor = Color.White
        If CDec(CalcPadDisplay.Text) = 0 _
            Or PendingOpFlag = True Then
            CalcPadDisplay.Text = sender.tag
            PendingOpFlag = False
        Else
            CalcPadDisplay.Text &= sender.tag
        End If
    End Sub
    Private Function AccumCalc(ByRef opcode As String)
        Select Case opcode
            Case "Divide"
                If CDec(CalcPadDisplay.Text) <> 0 Then
                    AccumCalc = _
                        Accum / CDbl(CalcPadDisplay.Text)
                Else
                    CalcPadDisplay.BackColor = Color.Red
                    AccumCalc = 0
                    PendingOp = "Null"
                    Exit Function
                End If
            Case "Multiply"
                AccumCalc = Accum * CDbl(CalcPadDisplay.Text)
            Case "Add"
                AccumCalc = Accum + CDbl(CalcPadDisplay.Text)
            Case "Subtract"
                AccumCalc = Accum - CDbl(CalcPadDisplay.Text)
        End Select
    End Function
End Class
Explore Visual Basic
By Category
About.com Special Features

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

Easy ways to connect two computers for networking purposes. More >

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

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

All rights reserved.