求VB计算器程序编成高手帮下忙...不光是+.-.X./4个运算符.运算符越多越好..需要全部程序,我们交作业必须完成呀....先谢谢了.^_^
我知道可以用代码实现,就是不知道代码..~_~#....麻烦高手帮帮忙..Thank you
我有个源程序,跟真的计算器差不多,要的话加QQ329796413.
或着你把你的邮箱给我我发给你!
或着你把你的邮箱给我我发给你!
温馨提示:答案为网友推荐,仅供参考
第1个回答 2008-07-02
这些功能用代码都可以实现的
第2个回答 2008-07-02
先建立一个新的项目文件,里面包含一个Form1的窗体。然后,添加控件,包括一个Text框(Text1),和17个按钮,这17个按钮使用的名称都是Command1,只是Index属性不同,就好象一个数组,创立一组按钮的方法是,先在窗体上建立一个按钮(Command1),然后在Command1上击鼠标右键,选择复制(Copy),然后在窗体上任意部位击右键,选择粘贴(Paste),系统会提示是否创建控件组,选择“是”,重复上面步骤,直至创立17个按钮为止。各控件属性如下表:
Text1
BackColor=&H00C0FFFF&
Text=“0.”
Command1
Index 0――16
Index 0――9 Caption=“0”-“9”
Index 10 Caption=“.”
Index 11Caption=“=”
Index 12 Caption=“+”
Index 13 Caption=“-”
Index 14 Caption=“×”
Index 15 Caption=“÷”
Index 16 Caption=“清除”
如果想改变一些其他的属性,比如字体,可以自己定义一下Font属性,如果相对多个控件的共同属性进行改变,可以在窗体上按下鼠标,拖动,把多个控件一起选定,然后对它们的共同属性进行修改。
别看这个东西小,还费了近我两个小时呢,不觉大叹手生,里面也有些地方很绕弯子,我尽量解释清楚。
在编写程序之前,我们先来回顾一下计算器的使用。
按下数字键在液晶屏上会出现数字,这是最基本的功能。
第一次按下运算键,再按下数字键,将重新输入第二个数字,再次按下运算键,将算出前两个数的结果,然后按下数字键,将又重新输入新的数字。
输入数字后,按下“等于键”将仍输出原来的数字,输入运算符和第二个数字后在按下“等于”,将得到二者的结果。然后又重复第二、三步。
按下“清除”,数据被全部清除。
了解了这些,我们一步一步实现。
先实现数据的输入,请看代码。
与未创立组的控件相比,它的事件里多了Index参数,组里所有的对象都使用同一个事件。
先在General里声明窗体级全局变量:
Dim Num1, Num2 As Single
Dim StrNum1, StrNum2 As String
Dim FirstNum As Boolean '判断是否是数字开头
因为Text1里是存放字符串的,所以要把输入的数据进行运算,必须把字符型变量转变为实型变量,分别用Num1、Num2和StrNum1、StrNum2进行存放。
在Form_Load事件里给变量初赋值:
Num1 = 0
Num2 = 0
StrNum1 = ""
StrNum2 = ""
FirstNum = True
因为17个按钮使用的是同一事件,所以我们用一个Select Case语句区分各个按钮,双击任意按钮输入代码:
Private Sub Command1_Click(Index As Integer)
Select Case Index
Case 0 To 9
If FirstNum Then
StrNum1 = Str(Index)’实型转字符型
FirstNum = False’初赋值为假
Else’非初赋值
StrNum1 = StrNum1 + Str(Index)
End If
Text1.Text = StrNum1’显示
<还有其他语句,等待插入>
End Select
End Sub
运行程序的话已经可以输入数据,下面我们来处理小数点。
在General里声明新的变量:
Dim PointFlag As Boolean '判断是否已有小数点
我们知道小数点只能输入一次,一个数据中不能有多个小数点存在,所以需要一个布尔变量做判断。
在Form_Load里进行初赋值:
PointFlag = False
在Command的单击事件里接着上面输入新的代码:
Case 10
If Not PointFlag Then’如果没有小数点
If FirstNum Then’如果是第一个字符
StrNum1 = "0."
FirstNum = False
Else
StrNum1 = StrNum1 + "."
End If
Else’已有小数点
Exit Sub’则退出单击事件
End If
PointFlag = True
Text1.Text = StrNum1
先在General里声明新变量:
Dim Runsign As Integer '储存运算符号
Dim SignFlag As Boolean '判断是否已有运算符号
然后在Form_Load中进行初赋值:
Runsign = 0
SignFlag = False
在接下的Command1_Click中继续输入代码:
Case 12 To 15
FirstNum = True’使下一次可以输入新数据
PointFlag = False
'还原标记值
If SignFlag Then’前面已有运算符未运算
Call Run
Else
SignFlag = True
StrNum2 = StrNum1’把字符串1复制给字符串2
StrNum1 = ""’字符串1清空
End If
Runsign = Index - 11 ‘储存键入的运算符
里面运算的部分我用了一个过程,是因为在按下等号时还会调用到运算过程,这样可以减少代码的输入,也使程序便于浏览。建立过程(Procedure),可以在General部分直接输入,也可以通过菜单里的“Add Procedure”添加。Run的代码如下:
Sub Run()
Num1 = Val(StrNum2)
Num2 = Val(StrNum1)
Select Case Runsign
Case 1’加
equal = Num1 + Num2
Case 2’减
equal = Num1 - Num2
Case 3’乘
equal = Num1 * Num2
Case 4’除
equal = Num1 / Num2
End Select
StrNum2 = Str(equal)
StrNum1 = StrNum2
Text1.Text = StrNum2
End Sub
下面是其余的代码:(在Command1_Click事件里,接着写)
Case 11
If Not SignFlag Then
Text1.Text = StrNum1
equal = Val(StrNum1)
FirstNum = True
PointFlag = False
Else
Call Run
SignFlag = False
End If
Case Else’清除按钮
Call ClearData
里面又用到了一个ClearData过程,实际上就是Form_Load里的变量初赋值,代码如下:
Sub ClearData()
Num1 = 0
Num2 = 0
StrNum1 = ""
StrNum2 = ""
FirstNum = True
PointFlag = False
Runsign = 0
SignFlag = False
Text1.Text = "0."
End Sub
这样完整的程序就完成了,本章介绍了VB的第一个控件――按钮,并且举例,希望你继续看下一章――菜单。
下面给出完整的程序代码:
Dim Num1, Num2 As Single
Dim StrNum1, StrNum2 As String
Dim FirstNum As Boolean '判断是否是数字开头
Dim PointFlag As Boolean '判断是否已有小数点
Dim Runsign As Integer '储存运算符号
Dim SignFlag As Boolean '判断是否已有运算符号
Sub ClearData()
Num1 = 0
Num2 = 0
StrNum1 = ""
StrNum2 = ""
FirstNum = True
PointFlag = False
Runsign = 0
SignFlag = False
Text1.Text = "0."
End Sub
Sub Run()
Num1 = Val(StrNum2)
Num2 = Val(StrNum1)
Select Case Runsign
Case 1
equal = Num1 + Num2
Case 2
equal = Num1 - Num2
Case 3
equal = Num1 * Num2
Case 4
equal = Num1 / Num2
End Select
StrNum2 = Str(equal)
StrNum1 = StrNum2
Text1.Text = StrNum2
End Sub
Private Sub about_Click()
MsgBox "Create by 周晓阳。1998/7/27", vbDefaultButton1, "VB教程第四章范例"
End Sub
Private Sub Command1_Click(Index As Integer)
Select Case Index
Case 0 To 9
If FirstNum Then
StrNum1 = Str(Index)
FirstNum = False
Else
StrNum1 = StrNum1 + Str(Index)
End If
Text1.Text = StrNum1
Case 10
If Not PointFlag Then
If FirstNum Then
StrNum1 = "0."
FirstNum = False
Else
StrNum1 = StrNum1 + "."
End If
Else
Exit Sub
End If
PointFlag = True
Text1.Text = StrNum1
Case 12 To 15
FirstNum = True
PointFlag = False
'还原标记值
If SignFlag Then
Call Run
Else
SignFlag = True
StrNum2 = StrNum1
StrNum1 = ""
End If
Runsign = Index - 11
Case 11
If Not SignFlag Then
Text1.Text = StrNum1
equal = Val(StrNum1)
FirstNum = True
PointFlag = False
Else
Call Run
SignFlag = False
End If
Case Else
Call ClearData
End Select
End Sub
Private Sub Form_Load()
Call ClearData
End Sub
代码直接复制 然后粘贴就能用了 ,加上几个控件!
Text1
BackColor=&H00C0FFFF&
Text=“0.”
Command1
Index 0――16
Index 0――9 Caption=“0”-“9”
Index 10 Caption=“.”
Index 11Caption=“=”
Index 12 Caption=“+”
Index 13 Caption=“-”
Index 14 Caption=“×”
Index 15 Caption=“÷”
Index 16 Caption=“清除”
如果想改变一些其他的属性,比如字体,可以自己定义一下Font属性,如果相对多个控件的共同属性进行改变,可以在窗体上按下鼠标,拖动,把多个控件一起选定,然后对它们的共同属性进行修改。
别看这个东西小,还费了近我两个小时呢,不觉大叹手生,里面也有些地方很绕弯子,我尽量解释清楚。
在编写程序之前,我们先来回顾一下计算器的使用。
按下数字键在液晶屏上会出现数字,这是最基本的功能。
第一次按下运算键,再按下数字键,将重新输入第二个数字,再次按下运算键,将算出前两个数的结果,然后按下数字键,将又重新输入新的数字。
输入数字后,按下“等于键”将仍输出原来的数字,输入运算符和第二个数字后在按下“等于”,将得到二者的结果。然后又重复第二、三步。
按下“清除”,数据被全部清除。
了解了这些,我们一步一步实现。
先实现数据的输入,请看代码。
与未创立组的控件相比,它的事件里多了Index参数,组里所有的对象都使用同一个事件。
先在General里声明窗体级全局变量:
Dim Num1, Num2 As Single
Dim StrNum1, StrNum2 As String
Dim FirstNum As Boolean '判断是否是数字开头
因为Text1里是存放字符串的,所以要把输入的数据进行运算,必须把字符型变量转变为实型变量,分别用Num1、Num2和StrNum1、StrNum2进行存放。
在Form_Load事件里给变量初赋值:
Num1 = 0
Num2 = 0
StrNum1 = ""
StrNum2 = ""
FirstNum = True
因为17个按钮使用的是同一事件,所以我们用一个Select Case语句区分各个按钮,双击任意按钮输入代码:
Private Sub Command1_Click(Index As Integer)
Select Case Index
Case 0 To 9
If FirstNum Then
StrNum1 = Str(Index)’实型转字符型
FirstNum = False’初赋值为假
Else’非初赋值
StrNum1 = StrNum1 + Str(Index)
End If
Text1.Text = StrNum1’显示
<还有其他语句,等待插入>
End Select
End Sub
运行程序的话已经可以输入数据,下面我们来处理小数点。
在General里声明新的变量:
Dim PointFlag As Boolean '判断是否已有小数点
我们知道小数点只能输入一次,一个数据中不能有多个小数点存在,所以需要一个布尔变量做判断。
在Form_Load里进行初赋值:
PointFlag = False
在Command的单击事件里接着上面输入新的代码:
Case 10
If Not PointFlag Then’如果没有小数点
If FirstNum Then’如果是第一个字符
StrNum1 = "0."
FirstNum = False
Else
StrNum1 = StrNum1 + "."
End If
Else’已有小数点
Exit Sub’则退出单击事件
End If
PointFlag = True
Text1.Text = StrNum1
先在General里声明新变量:
Dim Runsign As Integer '储存运算符号
Dim SignFlag As Boolean '判断是否已有运算符号
然后在Form_Load中进行初赋值:
Runsign = 0
SignFlag = False
在接下的Command1_Click中继续输入代码:
Case 12 To 15
FirstNum = True’使下一次可以输入新数据
PointFlag = False
'还原标记值
If SignFlag Then’前面已有运算符未运算
Call Run
Else
SignFlag = True
StrNum2 = StrNum1’把字符串1复制给字符串2
StrNum1 = ""’字符串1清空
End If
Runsign = Index - 11 ‘储存键入的运算符
里面运算的部分我用了一个过程,是因为在按下等号时还会调用到运算过程,这样可以减少代码的输入,也使程序便于浏览。建立过程(Procedure),可以在General部分直接输入,也可以通过菜单里的“Add Procedure”添加。Run的代码如下:
Sub Run()
Num1 = Val(StrNum2)
Num2 = Val(StrNum1)
Select Case Runsign
Case 1’加
equal = Num1 + Num2
Case 2’减
equal = Num1 - Num2
Case 3’乘
equal = Num1 * Num2
Case 4’除
equal = Num1 / Num2
End Select
StrNum2 = Str(equal)
StrNum1 = StrNum2
Text1.Text = StrNum2
End Sub
下面是其余的代码:(在Command1_Click事件里,接着写)
Case 11
If Not SignFlag Then
Text1.Text = StrNum1
equal = Val(StrNum1)
FirstNum = True
PointFlag = False
Else
Call Run
SignFlag = False
End If
Case Else’清除按钮
Call ClearData
里面又用到了一个ClearData过程,实际上就是Form_Load里的变量初赋值,代码如下:
Sub ClearData()
Num1 = 0
Num2 = 0
StrNum1 = ""
StrNum2 = ""
FirstNum = True
PointFlag = False
Runsign = 0
SignFlag = False
Text1.Text = "0."
End Sub
这样完整的程序就完成了,本章介绍了VB的第一个控件――按钮,并且举例,希望你继续看下一章――菜单。
下面给出完整的程序代码:
Dim Num1, Num2 As Single
Dim StrNum1, StrNum2 As String
Dim FirstNum As Boolean '判断是否是数字开头
Dim PointFlag As Boolean '判断是否已有小数点
Dim Runsign As Integer '储存运算符号
Dim SignFlag As Boolean '判断是否已有运算符号
Sub ClearData()
Num1 = 0
Num2 = 0
StrNum1 = ""
StrNum2 = ""
FirstNum = True
PointFlag = False
Runsign = 0
SignFlag = False
Text1.Text = "0."
End Sub
Sub Run()
Num1 = Val(StrNum2)
Num2 = Val(StrNum1)
Select Case Runsign
Case 1
equal = Num1 + Num2
Case 2
equal = Num1 - Num2
Case 3
equal = Num1 * Num2
Case 4
equal = Num1 / Num2
End Select
StrNum2 = Str(equal)
StrNum1 = StrNum2
Text1.Text = StrNum2
End Sub
Private Sub about_Click()
MsgBox "Create by 周晓阳。1998/7/27", vbDefaultButton1, "VB教程第四章范例"
End Sub
Private Sub Command1_Click(Index As Integer)
Select Case Index
Case 0 To 9
If FirstNum Then
StrNum1 = Str(Index)
FirstNum = False
Else
StrNum1 = StrNum1 + Str(Index)
End If
Text1.Text = StrNum1
Case 10
If Not PointFlag Then
If FirstNum Then
StrNum1 = "0."
FirstNum = False
Else
StrNum1 = StrNum1 + "."
End If
Else
Exit Sub
End If
PointFlag = True
Text1.Text = StrNum1
Case 12 To 15
FirstNum = True
PointFlag = False
'还原标记值
If SignFlag Then
Call Run
Else
SignFlag = True
StrNum2 = StrNum1
StrNum1 = ""
End If
Runsign = Index - 11
Case 11
If Not SignFlag Then
Text1.Text = StrNum1
equal = Val(StrNum1)
FirstNum = True
PointFlag = False
Else
Call Run
SignFlag = False
End If
Case Else
Call ClearData
End Select
End Sub
Private Sub Form_Load()
Call ClearData
End Sub
代码直接复制 然后粘贴就能用了 ,加上几个控件!
第3个回答 2008-07-02
Dim choice As String
Dim current As Double
Dim prev As Double
Private Const pi = 3.14159265
'equal 为=
'sigh 为+/-(正负号切换)
'cls为清零
'bs 为backspace
'plus为+
'minus为-
'asterisk为*
'slash为/
'num()为数字健控件数组
'point为小数点
Private Sub Form_Load()
Text1.Text = ""
prev = current = 0
End Sub
Private Sub num_Click(Index As Integer)
Text1.Text = Val(Text1.Text & num(Index).Caption)
current = Val(Text1.Text)
Private Sub sign_Click()
current = -current
Text1.Text = current
Private Sub point_Click()
If Text1.Text = "" Then
Text1.Text = "0."
Else
Text1.Text = Text1.Text & "."
End If
End Sub
Private Sub bs_Click()
Text1.Text = Mid$(Text1.Text, 1, (Len(Text1.Text) - 1))
Private Sub plus_Click()
Text1.Text = ""
choice = "+"
prev = current
Private Sub minus_Click()
Text1.Text = ""
choice = "-"
prev = current
Private Sub asterisk_Click()
Text1.Text = ""
choice = "*"
prev = current
Private Sub slash_Click()
Text1.Text = ""
choice = "/"
prev = current
Private Sub equal_Click()
If choice = "+" Then
current = prev + current
Text1.Text = current
ElseIf choice = "-" Then
current = prev - current
Text1.Text = current
ElseIf choice = "*" Then
current = prev * current
Text1.Text = current
ElseIf choice = "/" And current <> 0 Then
current = prev / current
Text1.Text = current
Else
MsgBox "除数不能为零", vbCritical + vbOKOnly + vbDefaultButton1, "出错"
End If
Private Sub cls_Click()
Text1.Text = ""
prev = current = 0
Dim current As Double
Dim prev As Double
Private Const pi = 3.14159265
'equal 为=
'sigh 为+/-(正负号切换)
'cls为清零
'bs 为backspace
'plus为+
'minus为-
'asterisk为*
'slash为/
'num()为数字健控件数组
'point为小数点
Private Sub Form_Load()
Text1.Text = ""
prev = current = 0
End Sub
Private Sub num_Click(Index As Integer)
Text1.Text = Val(Text1.Text & num(Index).Caption)
current = Val(Text1.Text)
Private Sub sign_Click()
current = -current
Text1.Text = current
Private Sub point_Click()
If Text1.Text = "" Then
Text1.Text = "0."
Else
Text1.Text = Text1.Text & "."
End If
End Sub
Private Sub bs_Click()
Text1.Text = Mid$(Text1.Text, 1, (Len(Text1.Text) - 1))
Private Sub plus_Click()
Text1.Text = ""
choice = "+"
prev = current
Private Sub minus_Click()
Text1.Text = ""
choice = "-"
prev = current
Private Sub asterisk_Click()
Text1.Text = ""
choice = "*"
prev = current
Private Sub slash_Click()
Text1.Text = ""
choice = "/"
prev = current
Private Sub equal_Click()
If choice = "+" Then
current = prev + current
Text1.Text = current
ElseIf choice = "-" Then
current = prev - current
Text1.Text = current
ElseIf choice = "*" Then
current = prev * current
Text1.Text = current
ElseIf choice = "/" And current <> 0 Then
current = prev / current
Text1.Text = current
Else
MsgBox "除数不能为零", vbCritical + vbOKOnly + vbDefaultButton1, "出错"
End If
Private Sub cls_Click()
Text1.Text = ""
prev = current = 0
第4个回答 2008-07-02
我写了半天,很辛苦啊!LZ!你看着办吧!对得起你良心就可以了!、
Option Explicit
Private StoredValue As Double
Private Const opNone = 0
Private Const opAdd = 1
Private Const opSubtract = 2
Private Const opMultiply = 3
Private Const opDivide = 4
Private Operator As Integer
Private NewEntry As Boolean
'删除最后的字符
Private Sub DeleteCharacter()
Dim txt As String
Dim min_len As Integer
txt = txtDisplay.Text
If Left$(txt, 1) = "-" Then
min_len = 2
Else
min_len = 1
End If
If Len(txt) > min_len Then
txtDisplay.Text = Left$(txt, Len(txt) - 1)
Else
txtDisplay.Text = "0"
End If
End Sub
'清除显示内容,保存运算符
Private Sub cmdClear_Click()
cmdClearEntry_Click
StoredValue = 0
Operator = opNone
End Sub
'清除显示内容
Private Sub cmdClearEntry_Click()
txtDisplay.Text = ""
End Sub
' 显示小数点
Private Sub cmdDecimal_Click()
If InStr(txtDisplay.Text, ".") Then
Beep
Else
If NewEntry Then
txtDisplay.Text = "."
NewEntry = False
Else
txtDisplay.Text = txtDisplay.Text & "."
End If
End If
End Sub
'计算上一操作符的运算结果
Private Sub cmdEquals_Click()
Dim new_value As Double
If txtDisplay.Text = "" Then
new_value = 0
Else
new_value = CDbl(txtDisplay.Text)
End If
Select Case Operator
Case opNone
StoredValue = new_value
Case opAdd
StoredValue = StoredValue + new_value
Case opSubtract
StoredValue = StoredValue - new_value
Case opMultiply
StoredValue = StoredValue * new_value
Case opDivide
StoredValue = StoredValue / new_value
End Select
Operator = opNone
NewEntry = True
txtDisplay.Text = Format$(StoredValue)
End Sub
' 显示数字
Private Sub cmdNumber_Click(Index As Integer)
If NewEntry Then
txtDisplay.Text = Format$(Index)
NewEntry = False
Else
txtDisplay.Text = txtDisplay.Text & Format$(Index)
End If
End Sub
Private Sub cmdOperator_Click(Index As Integer)
cmdEquals_Click
Operator = Index
NewEntry = True
End Sub
'改变符号
Private Sub cmdPlusMinus_Click()
If NewEntry Then
txtDisplay.Text = "-"
ElseIf Left$(txtDisplay.Text, 1) = "-" Then
txtDisplay.Text = Right$(txtDisplay.Text, 2)
Else
txtDisplay.Text = "-" & txtDisplay.Text
End If
End Sub
'处理键盘按键
Private Sub Form_KeyPress(KeyAscii As Integer)
txtDisplay_KeyPress KeyAscii
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
txtDisplay_KeyUp KeyCode, Shift
End Sub
Private Sub txtDisplay_Change()
txtDisplay.SelStart = Len(txtDisplay.Text)
End Sub
Private Sub txtDisplay_GotFocus()
txtDisplay_Change
End Sub
Private Sub txtDisplay_KeyPress(KeyAscii As Integer)
Dim ch As String
ch = Chr$(KeyAscii)
Select Case ch
Case "0"
cmdNumber_Click 0
Case "1"
cmdNumber_Click 1
Case "2"
cmdNumber_Click 2
Case "3"
cmdNumber_Click 3
Case "4"
cmdNumber_Click 4
Case "5"
cmdNumber_Click 5
Case "6"
cmdNumber_Click 6
Case "7"
cmdNumber_Click 7
Case "8"
cmdNumber_Click 8
Case "9"
cmdNumber_Click 9
Case "*", "x", "X"
cmdOperator_Click opMultiply
Case "+"
cmdOperator_Click opAdd
Case vbCrLf, vbCr, "="
cmdEquals_Click
Case "-"
cmdOperator_Click opSubtract
Case "."
cmdDecimal_Click
Case "/"
cmdOperator_Click opDivide
Case "C", "c"
cmdClearEntry_Click
End Select
KeyAscii = 0
End Sub
Private Sub txtDisplay_KeyUp(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyNumpad0
cmdNumber_Click 0
Case vbKeyNumpad1
cmdNumber_Click 1
Case vbKeyNumpad2
cmdNumber_Click 2
Case vbKeyNumpad3
cmdNumber_Click 3
Case vbKeyNumpad4
cmdNumber_Click 4
Case vbKeyNumpad5
cmdNumber_Click 5
Case vbKeyNumpad6
cmdNumber_Click 6
Case vbKeyNumpad7
cmdNumber_Click 7
Case vbKeyNumpad8
cmdNumber_Click 8
Case vbKeyNumpad9
cmdNumber_Click 9
Case vbKeyMultiply
cmdOperator_Click opMultiply
Case vbKeyAdd
cmdOperator_Click opAdd
Case vbKeySeparator
cmdEquals_Click
Case vbKeySubtract
cmdOperator_Click opSubtract
Case vbKeyDecimal
cmdDecimal_Click
Case vbKeyDivide
cmdOperator_Click opDivide
Case vbKeyBack, vbKeyDelete
DeleteCharacter
End Select
KeyCode = 0
End Sub
Option Explicit
Private StoredValue As Double
Private Const opNone = 0
Private Const opAdd = 1
Private Const opSubtract = 2
Private Const opMultiply = 3
Private Const opDivide = 4
Private Operator As Integer
Private NewEntry As Boolean
'删除最后的字符
Private Sub DeleteCharacter()
Dim txt As String
Dim min_len As Integer
txt = txtDisplay.Text
If Left$(txt, 1) = "-" Then
min_len = 2
Else
min_len = 1
End If
If Len(txt) > min_len Then
txtDisplay.Text = Left$(txt, Len(txt) - 1)
Else
txtDisplay.Text = "0"
End If
End Sub
'清除显示内容,保存运算符
Private Sub cmdClear_Click()
cmdClearEntry_Click
StoredValue = 0
Operator = opNone
End Sub
'清除显示内容
Private Sub cmdClearEntry_Click()
txtDisplay.Text = ""
End Sub
' 显示小数点
Private Sub cmdDecimal_Click()
If InStr(txtDisplay.Text, ".") Then
Beep
Else
If NewEntry Then
txtDisplay.Text = "."
NewEntry = False
Else
txtDisplay.Text = txtDisplay.Text & "."
End If
End If
End Sub
'计算上一操作符的运算结果
Private Sub cmdEquals_Click()
Dim new_value As Double
If txtDisplay.Text = "" Then
new_value = 0
Else
new_value = CDbl(txtDisplay.Text)
End If
Select Case Operator
Case opNone
StoredValue = new_value
Case opAdd
StoredValue = StoredValue + new_value
Case opSubtract
StoredValue = StoredValue - new_value
Case opMultiply
StoredValue = StoredValue * new_value
Case opDivide
StoredValue = StoredValue / new_value
End Select
Operator = opNone
NewEntry = True
txtDisplay.Text = Format$(StoredValue)
End Sub
' 显示数字
Private Sub cmdNumber_Click(Index As Integer)
If NewEntry Then
txtDisplay.Text = Format$(Index)
NewEntry = False
Else
txtDisplay.Text = txtDisplay.Text & Format$(Index)
End If
End Sub
Private Sub cmdOperator_Click(Index As Integer)
cmdEquals_Click
Operator = Index
NewEntry = True
End Sub
'改变符号
Private Sub cmdPlusMinus_Click()
If NewEntry Then
txtDisplay.Text = "-"
ElseIf Left$(txtDisplay.Text, 1) = "-" Then
txtDisplay.Text = Right$(txtDisplay.Text, 2)
Else
txtDisplay.Text = "-" & txtDisplay.Text
End If
End Sub
'处理键盘按键
Private Sub Form_KeyPress(KeyAscii As Integer)
txtDisplay_KeyPress KeyAscii
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
txtDisplay_KeyUp KeyCode, Shift
End Sub
Private Sub txtDisplay_Change()
txtDisplay.SelStart = Len(txtDisplay.Text)
End Sub
Private Sub txtDisplay_GotFocus()
txtDisplay_Change
End Sub
Private Sub txtDisplay_KeyPress(KeyAscii As Integer)
Dim ch As String
ch = Chr$(KeyAscii)
Select Case ch
Case "0"
cmdNumber_Click 0
Case "1"
cmdNumber_Click 1
Case "2"
cmdNumber_Click 2
Case "3"
cmdNumber_Click 3
Case "4"
cmdNumber_Click 4
Case "5"
cmdNumber_Click 5
Case "6"
cmdNumber_Click 6
Case "7"
cmdNumber_Click 7
Case "8"
cmdNumber_Click 8
Case "9"
cmdNumber_Click 9
Case "*", "x", "X"
cmdOperator_Click opMultiply
Case "+"
cmdOperator_Click opAdd
Case vbCrLf, vbCr, "="
cmdEquals_Click
Case "-"
cmdOperator_Click opSubtract
Case "."
cmdDecimal_Click
Case "/"
cmdOperator_Click opDivide
Case "C", "c"
cmdClearEntry_Click
End Select
KeyAscii = 0
End Sub
Private Sub txtDisplay_KeyUp(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyNumpad0
cmdNumber_Click 0
Case vbKeyNumpad1
cmdNumber_Click 1
Case vbKeyNumpad2
cmdNumber_Click 2
Case vbKeyNumpad3
cmdNumber_Click 3
Case vbKeyNumpad4
cmdNumber_Click 4
Case vbKeyNumpad5
cmdNumber_Click 5
Case vbKeyNumpad6
cmdNumber_Click 6
Case vbKeyNumpad7
cmdNumber_Click 7
Case vbKeyNumpad8
cmdNumber_Click 8
Case vbKeyNumpad9
cmdNumber_Click 9
Case vbKeyMultiply
cmdOperator_Click opMultiply
Case vbKeyAdd
cmdOperator_Click opAdd
Case vbKeySeparator
cmdEquals_Click
Case vbKeySubtract
cmdOperator_Click opSubtract
Case vbKeyDecimal
cmdDecimal_Click
Case vbKeyDivide
cmdOperator_Click opDivide
Case vbKeyBack, vbKeyDelete
DeleteCharacter
End Select
KeyCode = 0
End Sub