|
I have programmed this calculate that resembles a typical calculator that consists of the number buttons, the operator buttons and some additional buttons such as the memory button and the clear button. I have tested it and it performs just as good as the Microsoft Windows's calculator.To design the interface, you need to insert 25 command buttons, and one label that functions as the display panel. The number buttons from 1 to 9 are grouped together as a control array and named as ButtonNum while 0 is a standalone command and named as Bzero. The four basic operators are also grouped together as a control array and named as Operator. Other buttons are named appropriately according to their functions. The label is named as panel.One of the most important procedures in the program is to control the display on the panel. The procedure is
The Complete code:
Option Explicit Dim Num_of_digit As Integer Dim key As Integer Dim displayValue As Variant Dim a, b, c, d, e, f, g As Variant Dim memo As Variant Dim newNumber As Boolean
Private XE "Private" Sub BZero_Click XE "Click" (Index As Integer) If Num_of_digit > 0 Then panel.Caption = panel.Caption + "0" Else panel.Caption = "0" Num_of_digit = Num_of_digit + 1 End If CheckValue End Sub
Sub CheckValue() displayValue = Val(panel.Caption) End Sub
Private XE "Private" Sub ButtonNum_Click XE "Click" (Index As Integer) If Num_of_digit > 0 Then If Num_of_digit < 30 Then panel.Caption = panel.Caption + Right$(Str(Index), 1) Num_of_digit = Num_of_digit + 1 End If Else panel.Caption = Right$(Str(Index), 1) Num_of_digit = 1 End If CheckValue End Sub
Private XE "Private" Sub Clear_Click XE "Click" () panel.Caption = "0" displayValue = "0" Num_of_digit = 0 End Sub
Private XE "Private" Sub ClearAll_Click XE "Click" () panel.Caption = "0" displayValue = "0" memo = 0 End Sub Private XE "Private" Sub Equal_Click XE "Click" () CheckValue If newNumber = True Then If key = 1 Then e = displayValue + a ElseIf key = 2 Then e = b - displayValue ElseIf key = 3 Then e = displayValue * c ElseIf key = 5 Then e = (f * displayValue) / 100 ElseIf key = 4 And displayValue <> 0 Then e = d / displayValue Else GoTo error End If If Abs(e) < 1 Then panel.Caption = Format(e, "General Number") Else panel.Caption = Str(e) End If Else panel.Caption = displayValue End If GoTo finish error: panel.Caption = "E" finish: Num_of_digit = 0 newNumber = False End Sub
Private XE "Private" Sub MemoCancel_Click XE "Click" () memo = 0 End Sub
Private XE "Private" Sub Memory_Click XE "Click" () CheckValue memo = displayValue Num_of_digit = 0 End Sub
Private XE "Private" Sub Operator_Click XE "Click" (Index As Integer) CheckValue If Index = 11 Then a = displayValue key = 1 ElseIf Index = 12 Then b = displayValue key = 2 ElseIf Index = 13 Then c = displayValue key = 3 ElseIf Index = 14 Then d = displayValue key = 4
ElseIf Index = 15 Then f = displayValue key = 5 End If Num_of_digit = 0 newNumber = True End Sub
Private XE "Private" Sub Plus_minus_Click XE "Click" () CheckValue g = -1 * displayValue displayValue = g panel.Caption = Str(displayValue) CheckValue End Sub
Private XE "Private" Sub Poin_Click XE "Click" () Static point_lock As Integer If point_lock = 0 And Num_of_digit < 20 Then panel.Caption = panel.Caption + "." Num_of_digit = Num_of_digit + 1 End If CheckValue End Sub
Private XE "Private" Sub Recall_Click XE "Click" () panel.Caption = Str(memo) End Sub
Private XE "Private" Sub SqRoot_Click XE "Click" () CheckValue If displayValue >= 0 Then panel.Caption = Str(Sqr(displayValue)) Else panel.Caption = "E" End If Num_of_digit = 0 End Sub Private XE "Private" Sub Summation_Click XE "Click" () CheckValue memo = memo + displayValue Num_of_digit = 0 End Sub
. |