You are on page 1of 6

Method to Create Digital Calculator

Object : Form
Caption = "CALCULATOR"
Name = “frmCalc”

Object :CommandButton
Name = cmdNeg
Caption = "+/-"

Object :CommandButton
Name = cmdEqual
Caption = "="

Object :CommandButton
Name = cmdInvert
Caption = "1/X"

Object :CommandButton
Name = cmdExp
Caption = "^"

Object :CommandButton
Name = cmdDiv
Caption = "/"

Object :CommandButton
Name = cmdMul
Caption = "*"

Object :CommandButton
Name = cmdMinus
Caption = "-"

Object :CommandButton
Name = cmdPlus
Caption = "+"

Object :CommandButton
Name = cmdClear
Caption = "C"

Object :CommandButton
Name = cmdDot
Caption = "."

Object :CommandButton
Name = cmdDigit
Caption = "0"
Index = 9

Object :CommandButton
Name = cmdDigit
Caption = "1"
Index = 8

Object :CommandButton
Name = cmdDigit
Caption = "2"
Index = 7

Object :CommandButton
Name = cmdDigit
Caption = "3"
Index = 6

Object :CommandButton
Name = cmdDigit
Caption = "4"
Index = 5

Object :CommandButton
Name = cmdDigit
Caption = "5"
Index = 4

Object :CommandButton
Name = cmdDigit
Caption = "6"
Index = 3

Object :CommandButton
Name = cmdDigit
Caption = "7"
Height = 495
Index = 2

Object :CommandButton
Name = cmdDigit
Caption = "8"
Index = 1

Object :CommandButton
Name = cmdDigit
Caption = "9"
Index = 0

Object :CommandButton
Name = lblDisplay
Alignment = 1 'Right Justify
BackColor = &H80000009&
BorderStyle = 1 'Fixed Single

Dim Op1 As Integer


Dim Op2 As Integer
Dim Oper As String

Private Sub cmdClear_Click()


lblDisplay.Caption = ""
End Sub

Private Sub cmdDigit_Click(Index As Integer)


lblDisplay.Caption = lblDisplay.Caption & CmdDigit(Index).Caption
End Sub

Private Sub cmdDiv_Click()


If lblDisplay.Caption <> "" Then
Op1 = lblDisplay.Caption
lblDisplay.Caption = ""
Oper = "/"
End If
End Sub

Private Sub cmdEqual_Click()


If lblDisplay.Caption <> "" Then
Op2 = lblDisplay.Caption
End If

Select Case Oper

Case "+"
lblDisplay.Caption = Op1 + Op2

Case "-"
lblDisplay.Caption = Op1 - Op2
Case "*"
lblDisplay.Caption = Op1 * Op2
Case "/"
If Op2 <> 0 Then
lblDisplay.Caption = Op1 / Op2
Else
lblDisplay.Caption = "E"
End If
Case "^"
lblDisplay.Caption = Op1 ^ Op2

End Select

End Sub

Private Sub cmdExp_Click()


If lblDisplay.Caption <> "" Then
Op1 = lblDisplay.Caption
lblDisplay.Caption = ""
Oper = "^"
End If
End Sub

Private Sub cmdInvert_Click()


If lblDisplay.Caption <> "" And lblDisplay.Caption <> "0" Then
lblDisplay.Caption = 1 / lblDisplay.Caption
Else
lblDisplay.Caption = "E"
End If
End Sub

Private Sub cmdMinus_Click()


If lblDisplay.Caption <> "" Then
Op1 = lblDisplay.Caption
lblDisplay.Caption = ""
Oper = "-"
End If
End Sub

Private Sub cmdMod_Click()


If lblDisplay.Caption <> "" Then
Op1 = lblDisplay.Caption
lblDisplay.Caption = ""
Oper = "%"
End If

End Sub

Private Sub cmdMul_Click()


If lblDisplay.Caption <> "" Then
Op1 = lblDisplay.Caption
lblDisplay.Caption = ""
Oper = "*"
End If
End Sub

Private Sub cmdNeg_Click()


If lblDisplay.Caption <> "" Then
lblDisplay.Caption = -lblDisplay.Caption
End If
End Sub

Private Sub cmdPlus_Click()


If lblDisplay.Caption <> "" Then
Op1 = lblDisplay.Caption
lblDisplay.Caption = ""
Oper = "+"
End If
End Sub

Private Sub cmdDot_Click()


If InStr(1, lblDisplay.Caption, ".") = 0 Then
lblDisplay.Caption = lblDisplay.Caption + "."
End If
End Sub

You might also like