📄 frmcalculator.frm
字号:
VERSION 5.00
Begin VB.Form frmcalculator
BorderStyle = 1 'Fixed Single
Caption = "计算器"
ClientHeight = 4050
ClientLeft = 45
ClientTop = 375
ClientWidth = 4065
LinkTopic = "Form1"
MaxButton = 0 'False
MinButton = 0 'False
ScaleHeight = 4050
ScaleWidth = 4065
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton cmdback
Caption = "-->"
Height = 615
Left = 2400
TabIndex = 18
Top = 960
Width = 1335
End
Begin VB.CommandButton cmdequal
Caption = "="
BeginProperty Font
Name = "宋体"
Size = 15.75
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 615
Left = 2400
TabIndex = 17
Top = 3120
Width = 1335
End
Begin VB.CommandButton cmdpn
Caption = "+/-"
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 615
Left = 1680
TabIndex = 16
Top = 3120
Width = 615
End
Begin VB.CommandButton cmdcalc
Caption = "/"
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 615
Index = 3
Left = 3120
TabIndex = 15
Top = 2400
Width = 615
End
Begin VB.CommandButton cmdcalc
Caption = "*"
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 615
Index = 2
Left = 2400
TabIndex = 14
Top = 2400
Width = 615
End
Begin VB.CommandButton cmdcalc
Caption = "-"
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 615
Index = 1
Left = 3120
TabIndex = 13
Top = 1680
Width = 615
End
Begin VB.CommandButton cmdcalc
Caption = "+"
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 615
Index = 0
Left = 2400
TabIndex = 12
Top = 1680
Width = 615
End
Begin VB.CommandButton cmdnumber
Caption = "."
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 615
Index = 10
Left = 960
TabIndex = 11
Top = 3120
Width = 615
End
Begin VB.CommandButton cmdnumber
Caption = "9"
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 615
Index = 9
Left = 1680
TabIndex = 10
Top = 2400
Width = 615
End
Begin VB.CommandButton cmdnumber
Caption = "8"
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 615
Index = 8
Left = 960
TabIndex = 9
Top = 2400
Width = 615
End
Begin VB.CommandButton cmdnumber
Caption = "7"
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 615
Index = 7
Left = 240
TabIndex = 8
Top = 2400
Width = 615
End
Begin VB.CommandButton cmdnumber
Caption = "6"
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 615
Index = 6
Left = 1680
TabIndex = 7
Top = 1680
Width = 615
End
Begin VB.CommandButton cmdnumber
Caption = "5"
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 615
Index = 5
Left = 960
TabIndex = 6
Top = 1680
Width = 615
End
Begin VB.CommandButton cmdnumber
Caption = "4"
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 615
Index = 4
Left = 240
TabIndex = 5
Top = 1680
Width = 615
End
Begin VB.CommandButton cmdnumber
Caption = "3"
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 615
Index = 3
Left = 1680
TabIndex = 4
Top = 960
Width = 615
End
Begin VB.CommandButton cmdnumber
Caption = "2"
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 615
Index = 2
Left = 960
TabIndex = 3
Top = 960
Width = 615
End
Begin VB.CommandButton cmdnumber
Caption = "1"
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 615
Index = 1
Left = 240
TabIndex = 2
Top = 960
Width = 615
End
Begin VB.CommandButton cmdnumber
Caption = "0"
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 615
Index = 0
Left = 240
TabIndex = 1
Top = 3120
Width = 615
End
Begin VB.TextBox txtdisplay
Alignment = 1 'Right Justify
BackColor = &H80000011&
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 735
Left = 240
MultiLine = -1 'True
TabIndex = 0
Top = 120
Width = 3495
End
End
Attribute VB_Name = "frmcalculator"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim str1 As String, str2 As String '以字符串形式存放两个操作数
Dim strtemp As String '暂存字符串
Dim blnstatus As Boolean 'false时,第一个操作数;true时,第二个操作数
Dim intoperate As Integer '四则运算控件数组的click 事件过程
Private Sub cmdback_Click() '删除最后一个数字
If Not blnstatus Then strtemp = str1 Else strtemp = str2
If Len(strtemp) > 0 Then
strtemp = Left(strtemp, Len(strtemp) - 1)
End If
txtdisplay.Text = strtemp
If Not blnstatus Then str1 = strtemp Else str2 = strtemp
End Sub
Private Sub cmdcalc_Click(Index As Integer) '四则运算控件数组的click事件
str2 = "" '清空第二个字符串
txtdisplay.Text = ""
blnstatus = True '为输入第二个字符串做准备
intoperate = Index
End Sub
Private Sub cmdequal_Click() '单击了等号时进行运算
If Len(Trim(str1)) = 0 Or Len(Trim(str2)) = 0 Then '任一字符串为空时则不计算
blnstatus = False
Exit Sub
End If
Dim dbl1 As Double
Dim dbl2 As Double
dbl1 = Val(str1) '把字符串转换为数值
dbl2 = Val(str2)
Select Case intoperate '进行不同的四则运算
Case 0
txtdisplay.Text = dbl1 + dbl2
Case 1
txtdisplay.Text = dbl1 - dbl2
Case 2
txtdisplay.Text = dbl1 * dbl2
Case 3
If dbl2 = 0 Then
MsgBox "除数不能为0!", 64, "计算器"
Exit Sub
End If
txtdisplay.Text = dbl1 / dbl2
End Select
blnstatus = False '为输入第一个数做准备
str1 = "" '清空两个字符串
str2 = ""
End Sub
Private Sub cmdnumber_Click(Index As Integer)
If Not blnstatus Then strtemp = str1 Else strtemp = str2
If Index < 10 Then '输入数字
strtemp = strtemp + CStr(Index)
Else '输入小数点,防止出现两个小数点
If InStr(strtemp, ".") = 0 Then strtemp = strtemp + "."
End If
If Len(strtemp) > 1 And Left(strtemp, 1) = "0" And Mid(strtemp, 2, 1) <> "." Then
strtemp = Right(strtemp, Len(strtemp) - 1) '删除数开始多余的0
End If
txtdisplay.Text = strtemp
If Not blnstatus Then str1 = strtemp Else str2 = strtemp
End Sub
Private Sub cmdpn_Click() '改变正负号
If Len(strtemp) > 0 And Left(strtemp, 1) = "-" Then
strtemp = Right(strtemp, Len(strtemp) - 1) '去掉负号
Else
strtemp = "-" & strtemp '添加负号
End If
txtdisplay.Text = strtemp
If Not blnstatus Then str1 = strtemp Else str2 = strtemp
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -