参考下:
计算器的程序,很简单,只弄了+-法,乘除没弄,只要把代码看懂照着+-法改一下就行了,不过涉及到多级运算只用一个LastClick是不行的
程序的控件就两种
CommandBotton和TextBox都是最基本的
先创建十一个CommandBotton,Caption改为0-9以及一个"."(小数点)
创建三个CommandBotton,Caption改为“-”,“+”“=”
创建五个CommandBotton,Caption改为“CM”(clearmemory) “MR”(memoryrecall) “MS”(memorysave) “C”(clear) “AC”(allclear)
再创建个TextBox
下面的是对应代码,很清楚
不懂来问我`~
Dim Counter As Double ‘这个用来记录结果
Dim Memory As Double ‘用来记录你保存的数,这个我们用的科学计算器里也有
Dim Reg As Double ‘这个也是用来记录结果
Dim CalcNumber As Long ‘这个是判断:1当之前点过一次+或-时再点一次运算结果,2如果前面没点过+或-则不进行运算
Dim DisplayFromNew As Boolean ‘这个用来判断输入框状态:是接着输入还是从新输入新的数
Dim AlreadyDecimalThere As Boolean ‘这个用来判断是否能用小数点
Dim LastClick As String ‘这个用来记录最后一次选择的运算法则
Private Sub Form_Load()
Display.Text = "0"
Reg = 0
CalcNumber = 0
DisplayFromNew = True
AlreadyDecimalThere = False
End Sub
Private Sub AllClear_Click() ‘这个不用说了,是全部清空
DisplayFromNew = True ‘恢复到初始状态
Counter = 0
Display.Text = "0" ‘清空输入框
CalcNumber = 0
Reg = 0
AlreadyDecimalThere = False ‘恢复到初始状态
End Sub
Private Sub Clear_Click() ‘清空,但不清空纪录
DisplayFromNew = True
Display.Text = "0"
End Sub
Private Sub ClearMemory_Click() ‘清空纪录
Memory = 0
End Sub
Private Sub Decimal_Click() ‘关于小数点的
If AlreadyDecimalThere = False Then
AddDisplayDigit "."
AlreadyDecimalThere = True ‘每次输入只能输一次小数点
End If
End Sub
Private Sub Equals_Click() ‘等号时的
If LastClick = "Minus" Then
If CalcNumber = 0 Then
Reg = CDbl(Display.Text) ‘记录
DisplayFromNew = True
CalcNumber = 1
Else ‘这个的意思是,如果只点了法则(+或-)之后没点数,就加上前面输的数,但我觉得这样就等于前面输得数的2倍(+时)或0(-时),所以不用这么麻烦
Counter = Reg - CDbl(Display.Text)
Display.Text = CStr(Counter)
DisplayFromNew = True
AlreadyDecimalThere = False
Reg = CDbl(Display.Text)
End If
ElseIf LastClick = "Addition" Then ‘这是加法,和减法一样
If CalcNumber = 0 Then
Reg = CDbl(Display.Text)
DisplayFromNew = True
CalcNumber = 1
Else
Counter = Reg + CDbl(Display.Text)
Display.Text = CStr(Counter)
DisplayFromNew = True
AlreadyDecimalThere = False
Reg = CDbl(Display.Text)
End If
End If
CalcNumber = 0
End Sub
Sub AddDisplayDigit(Addition As String) ‘这一段十分重要,也是大家要学习的,就是自己创建一个函数,这样应用起来会十分方便
If DisplayFromNew = True Then
Display.Text = Addition ‘当DisplayFromNew = True时(大家注意杂那几种情况下= True)输入栏从新显示刚输入的数
DisplayFromNew = False ‘之后立即变为False,因为要接着这个数继续输入
Else
Display.Text = Display.Text & Addition ‘当DisplayFromNew = False 就是接着输入栏里已有的数字继续输入
End If
End Sub
注:为何要弄两种情况呢,因为当点击-或+时,要求输入栏从新记录
Private Sub MemorySave_Click() ‘这是保存记录
If DisplayFromNew = False Then
Memory = CDbl(Display.Text)
End If
End Sub
Private Sub MemoryRecall_Click() ‘打开纪录
Display = CStr(Memory)
End Sub
Private Sub Minus_Click() ‘点击
If CalcNumber = 0 Then
Reg = CDbl(Display.Text)
DisplayFromNew = True
CalcNumber = 1
Else ‘这个就是如果前面输过一次-号,再点-号就运算结果,这一点是很重要的,如果不加这个就无法进行连续运算 Counter = Reg - CDbl(Display.Text)
Display.Text = CStr(Counter)
DisplayFromNew = True
AlreadyDecimalThere = False
Reg = CDbl(Display.Text)
End If
LastClick = "Minus"
End Sub
Private Sub Plus_Click() ‘和减法一样
If CalcNumber = 0 Then
Reg = CDbl(Display.Text)
DisplayFromNew = True
CalcNumber = 1
Else
Counter = Reg + CDbl(Display.Text)
Display.Text = CStr(Counter)
DisplayFromNew = True
AlreadyDecimalThere = False
Reg = CDbl(Display.Text)
End If
LastClick = "Addition"
End Sub
‘下面的就简单了0-9,不过用到了前面定义的函数AddDisplayDigit,大家看到这个自定义函数的作用了吧
Private Sub Nine_Click()
AddDisplayDigit "9"
End Sub
Private Sub Eight_Click()
AddDisplayDigit "8"
End Sub
Private Sub Seven_Click()
AddDisplayDigit "7"
End Sub
Private Sub Six_Click()
AddDisplayDigit "6"
End Sub
Private Sub Five_Click()
AddDisplayDigit "5"
End Sub
Private Sub Four_Click()
AddDisplayDigit "4"
End Sub
Private Sub Three_Click()
AddDisplayDigit "3"
End Sub
Private Sub Two_Click()
AddDisplayDigit "2"
End Sub
Private Sub One_Click()
AddDisplayDigit "1"
End Sub
Private Sub Zero_Click()
AddDisplayDigit "0"
End Sub