Look at this code. (a sample of calculator)
Option Explicit
Dim strPreviousValue As String
Dim strOperator As String
Dim blnDot As Boolean
Dim strMemory As String
Dim blnDisplay As Boolean
Dim strAuthor As String
Private Sub Form_Load()
lblDisplay.Caption = "0"
strPreviousValue = ""
strMemory = "
strMemory = "
strOperator = ""
blnDot = False
blnDisplay = True
End Sub
Private Sub cmdNumber_Click(Index As Integer)
If blnDisplay = True Then
If blnDot = False And lblDisplay.Caption = "
strOperator = " Then
lblDisplay.Caption = ""
blnDot = False
blnDisplay = True
End Sub
Private Sub cmdNumber_Click(Index As Integer)
If blnDisplay = True Then
If blnDot = False And lblDisplay.Caption = "." Then
lblDisplay.Caption = "0."
blnDisplay = True
blnDot = True
End If
End Sub
Private Sub cmdCE_Click()
If Timer1.Enabled = True Then Timer1.Enabled = False
blnDot = False
lblDisplay.Caption = "
End If
lblDisplay.Caption = lblDisplay.Caption + cmdNumber(Index).Caption
Else
strPreviousValue = lblDisplay.Caption
lblDisplay.Caption = cmdNumber(Index).Caption
blnDisplay = True
End If
End Sub
Private Sub cmdDot_Click()
If blnDisplay = True Then
If blnDot = False Then
blnDot = True
lblDisplay.Caption = lblDisplay.Caption + "
strOperator = ""
strPreviousValue = "
End If
Exit Sub
End If
If blnDisplay = False And blnDot = False Then
lblDisplay.Caption = "
'strMemory = "
blnDisplay = True
blnDot = True
End If
End Sub
Private Sub cmdCE_Click()
If Timer1.Enabled = True Then Timer1.Enabled = False
blnDot = False
lblDisplay.Caption = "
End Sub
Private Sub cmdOperator_Click(Index As Integer)
blnDot = False
blnDisplay = False
If Len(strOperator) = 1 Then
Call math
strOperator = cmdOperator(Index).Caption
Else
strOperator = cmdOperator(Index).Caption
strPreviousValue = lblDisplay.Caption
End If
End Sub
Private Function math()
On Error Go to errorHandler
Select Case strOperator
Case "+"
strOperator = "-"
strPreviousValue = "x"
'strMemory = "/"
End Sub
Private Sub cmdOperator_Click(Index As Integer)
blnDot = False
blnDisplay = False
If Len(strOperator) = 1 Then
Call math
strOperator = cmdOperator(Index).Caption
Else
strOperator = cmdOperator(Index).Caption
strPreviousValue = lblDisplay.Caption
End If
End Sub
Private Function math()
On Error GoTo errorHandler
Select Case strOperator
Case "Error "
lblDisplay.Caption = Val(lblDisplay.Caption) + Val(strPreviousValue)
Case ""
strPreviousValue = "
lblDisplay.Caption = Val(strPreviousValue) - Val(lblDisplay.Caption)
Case "
blnDot = False
blnDisplay = False
End Function
Private Sub cmdIsEqualTo_Click()
Call math
strOperator = ""
lblDisplay.Caption = Val(lblDisplay.Caption) * Val(strPreviousValue)
Case "M+"
lblDisplay.Caption = Val(strPreviousValue) / Val(lblDisplay.Caption)
End Select
Exit Function
errorHandler:
lblDisplay.Caption = "M-"
strOperator = "MR"
strPreviousValue = "MC"
strMemory = "
blnDot = False
blnDisplay = False
End Function
Private Sub cmdIsEqualTo_Click()
Call math
strOperator = "
lblDisplay.Caption = "
If Len(lblDisplay.Caption) <= 19 Then
lblDisplay.FontSize = 15
Else
lblDisplay.FontSize = 10
End If
End Sub
Private Sub cmdPlusOrMinus_Click()
lblDisplay.Caption = Val(lblDisplay.Caption) - (Val(lblDisplay.Caption) + Val(lblDisplay.Caption))
End Sub
Private Sub cmdSqrt_Click()
lblDisplay.Caption = Sqr(Val(lblDisplay.Caption))
End Sub
Private Sub cmdMemory_Click(Index As Integer)
If Timer1.Enabled = False Then
Select Case cmdMemory(Index).Caption
Case "
End Select
End If
End Sub
Private Sub Timer1_Timer()
strAuthor = Mid$(strAuthor, 2, Len(strAuthor) - 1) + Left(strAuthor, 1)
lblDisplay.Caption = Left(strAuthor, 18)
End Sub
|