vb编程一个简单计算器(用VB编写一个简易的计算器)

本文目录
- 用VB编写一个简易的计算器
- 怎样用VB编写简单的计算器程序
- 如何利用VB做一个简单的计算器
- 用VB编写简单计算器程序
- 如何用VB编写最简单的计算器程序
- VB设计一个简易计算器
- 怎样用vb编程一部简单的计算器呢
- 用VB编程序设计一个简易的计算器 要求如下:
用VB编写一个简易的计算器
Dim
opt
As
String
Private
Sub
Command1_Click()
opt
=
“+“
End
Sub
Private
Sub
Command2_Click()
opt
=
“-“
End
Sub
Private
Sub
Command3_Click()
opt
=
“*“
End
Sub
Private
Sub
Command4_Click()
opt
=
“/“
End
Sub
Private
Sub
Command5_Click()
Dim
a,
b,
result
As
Double
a
=
Text1.Text
b
=
Text2.Text
Select
Case
opt
Case
“+“
result
=
Val(a)
+
Val(b)
Case
“-“
result
=
Val(a)
-
Val(b)
Case
“*“
result
=
Val(a)
*
Val(b)
Case
“/“
result
=
Val(a)
/
Val(b)
End
Select
Text3.Text
=
result
End
Sub
要做成上面的那个要花很多时间,所以只做了个自己输入数字的计算器。希望符合。
怎样用VB编写简单的计算器程序
其实比较简单啦,用一个窗体就可以实现啦!
我自己写的,你可以看看
Option Explicit
Dim strNumber As String
Dim strPoint As String
Dim dblNum1 As Double
Dim intOperator As Integer
’清除结果
Private Sub cmdGT_Click()
txtDisplay.Text = “0.“
strNumber = ““
strPoint = “.“
intOperator = 7
End Sub
’输入数字
Private Sub cmdNumber_Click(Index As Integer)
strNumber = strNumber & cmdNumber(Index).Caption
txtDisplay.Text = strNumber & strPoint
End Sub
Private Sub cmdOnOff_Click()
End
End Sub
’运算过程
Private Sub cmdOperator_Click(Index As Integer)
Dim dblnum2 As Double
’是第一次单击运算符时,将输入的值先赋给第一个数,否则赋值给第二个数进行运算
If intOperator = 7 Then
dblNum1 = CDbl(txtDisplay.Text)
Else
dblnum2 = CDbl(Val(txtDisplay.Text))
’根据输入的符号进行运算
’求普通运算
Select Case intOperator
Case 0
dblNum1 = dblNum1 + dblnum2
Case 1
dblNum1 = dblNum1 - dblnum2
Case 2
dblNum1 = dblNum1 * dblnum2
Case 3
If dblnum2 《》 0 Then
dblNum1 = dblNum1 / dblnum2
Else
MsgBox “除数不能为“0”!请重新输入除数。“, vbOKOnly + vbInformation, “除零错误“
Index = intOperator
End If
Case 6
dblNum1 = dblNum1 * dblnum2 / 100
End Select
End If
’取得当前输入的运算符,以做下次运算
intOperator = Index
strNumber = ““
txtDisplay = CStr(dblNum1)
’判断是否为文本框中的数字加点
If Not txtDisplay Like “*.*“ Then
txtDisplay.Text = txtDisplay.Text & “.“
End If
End Sub
Private Sub cmdOtherOper_Click(Index As Integer)
Dim dblNum As Double
’求平方根,平方,
dblNum = CDbl(Val(txtDisplay.Text))
Select Case Index
Case 0
’验证数据是否有效
If dblNum 》= 0 Then
txtDisplay.Text = CStr(Sqr(dblNum))
Else
MsgBox “负数不能开平方根!“, _
vbOKOnly + vbCritical, “开平方根错误“
End If
Case 1
txtDisplay.Text = CStr(dblNum ^ 2)
End Select
’判断是否为文本框中的数字加点
If Not txtDisplay Like “*.*“ Then
txtDisplay.Text = txtDisplay.Text & “.“
End If
End Sub
Private Sub cmdPoint_Click()
strNumber = strNumber & strPoint
strPoint = ““
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
’使被按下的数字键的对应按钮取得焦点
Select Case KeyCode
Case 48 To 57
cmdNumber(KeyCode - 48).SetFocus
Case 96 To 105
cmdNumber(KeyCode - 96).SetFocus
Case Else
’使按下的符号键对应的按钮取得焦点
If KeyCode = 107 Or (Shift = vbShiftMask And KeyCode = 187) Then
cmdOperator(0).SetFocus
cmdOperator_Click (0)
ElseIf KeyCode = 109 Or KeyCode = 189 Then
cmdOperator(1).SetFocus
cmdOperator_Click (1)
ElseIf KeyCode = 106 Or (Shift = vbShiftMask And KeyCode = 56) Then
cmdOperator(2).SetFocus
cmdOperator_Click (2)
ElseIf KeyCode = 111 Or KeyCode = 191 Then
cmdOperator(3).SetFocus
cmdOperator_Click (3)
ElseIf KeyCode = 13 Then
cmdOperator(7).SetFocus
cmdOperator_Click (7)
ElseIf KeyCode = 8 Then
cmdGT.SetFocus
Call cmdGT_Click
End If
End Select
End Sub
Private Sub Form_KeyPress(KeyAscii As Integer)
’将合法的数据输入到文本框
Select Case KeyAscii
Case 48 To 58
’调用数字键点击处理程序
cmdNumber_Click KeyAscii - 48
KeyAscii = 0
Case 46
’调用小数点输入
cmdPoint_Click
KeyAscii = 0
Case 13
’当敲击回车时,不能触发Form的 KeyUp 事件,因此在这里设置文本框的焦点
txtDisplay.SetFocus
Case Else
KeyAscii = 0
End Select
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
txtDisplay.SetFocus
End Sub
Private Sub Form_Load()
strNumber = ““
strPoint = “.“
intOperator = 7
End Sub
如何利用VB做一个简单的计算器
这是我给别人写过的简单计算器程序,送给你用吧。
后面发了两个文件的内容,你创建两个文本文件按照那两个文件命名后,并把对应代码用记事本打开粘贴到里面,最好再用vb运行project1.vbp文件编译成exe文件或直接执行都可以。
计算器真正手写代码就这么多。
Dim mem1 As Long, mem2 As Long, mem3 As Long
Dim calc As Integer
Private Sub Command1_Click(Index As Integer)
Text1.Text = Text1.Text & Index
End Sub
Private Sub Command2_Click(Index As Integer)
calc = Index
mem1 = Text1.Text: Text1.Text = ““
End Sub
Private Sub Command3_Click(Index As Integer)
Select Case Index
Case 0
Text1.Text = ““
mem1 = 0: mem2 = 0: mem3 = 0
Case 1
Text1.Text = Text1.Text & “.“
Case 2
mem2 = Text1.Text: Text1.Text = ““
Select Case calc
Case 0 ’+
mem3 = mem1 + mem2
Case 1 ’-
mem3 = mem1 - mem2
Case 2 ’*
mem3 = mem1 * mem2
Case 3 ’/
mem3 = mem1 / mem2
End Select
Text1.Text = mem3
End Select
End Sub
以下是计算器完整代码文件内容-----------------------------------
因为百度知道不能上传压缩,只好把工程文件的文本代码发给你了。
以下是文件Project1.vbp的内容
Type=Exe
Form=Form1.frm
Reference=*\G{00020430-0000-0000-C000-000000000046}#2.0#0#C:\WINDOWS\system32\STDOLE2.TLB#OLE Automation
Startup=“Form1“
Command32=““
Name=“Project1“
HelpContextID=“0“
CompatibleMode=“0“
MajorVer=1
MinorVer=0
RevisionVer=0
AutoIncrementVer=0
ServerSupportFiles=0
VersionCompanyName=“BaterStudio“
CompilationType=0
OptimizationType=0
FavorPentiumPro(tm)=0
CodeViewDebugInfo=0
NoAliasing=0
BoundsCheck=0
OverflowCheck=0
FlPointCheck=0
FDIVCheck=0
UnroundedFP=0
StartMode=0
Unattended=0
Retained=0
ThreadPerObject=0
MaxNumberOfThreads=1
[MS Transaction Server]
AutoRefresh=1
以下是文件Form1.frm里包括按钮信息的文件内容
VERSION 5.00
Begin VB.Form Form1
Caption = “Form1“
ClientHeight = 5835
ClientLeft = 60
ClientTop = 345
ClientWidth = 4380
LinkTopic = “Form1“
ScaleHeight = 5835
ScaleWidth = 4380
StartUpPosition = 3 ’Windows Default
Begin VB.CommandButton Command3
Caption = “=“
Height = 615
Index = 2
Left = 1920
TabIndex = 17
Top = 4440
Width = 615
End
Begin VB.CommandButton Command3
Caption = “.“
Height = 615
Index = 1
Left = 1200
TabIndex = 16
Top = 4440
Width = 615
End
Begin VB.CommandButton Command3
Caption = “C“
Height = 615
Index = 0
Left = 480
TabIndex = 15
Top = 4440
Width = 615
End
Begin VB.CommandButton Command2
Caption = “÷“
Height = 615
Index = 3
Left = 3360
TabIndex = 14
Top = 3120
Width = 615
End
Begin VB.CommandButton Command2
Caption = “ד
Height = 615
Index = 2
Left = 3360
TabIndex = 13
Top = 2400
Width = 615
End
Begin VB.CommandButton Command2
Caption = “-“
Height = 615
Index = 1
Left = 3360
TabIndex = 12
Top = 1680
Width = 615
End
Begin VB.CommandButton Command2
Caption = “+“
Height = 615
Index = 0
Left = 3360
TabIndex = 11
Top = 960
Width = 615
End
Begin VB.CommandButton Command1
Caption = “9“
Height = 615
Index = 9
Left = 1920
TabIndex = 10
Top = 960
Width = 615
End
Begin VB.CommandButton Command1
Caption = “8“
Height = 615
Index = 8
Left = 1200
TabIndex = 9
Top = 960
Width = 615
End
Begin VB.CommandButton Command1
Caption = “7“
Height = 615
Index = 7
Left = 480
TabIndex = 8
Top = 960
Width = 615
End
Begin VB.CommandButton Command1
Caption = “6“
Height = 615
Index = 6
Left = 1920
TabIndex = 7
Top = 1680
Width = 615
End
Begin VB.CommandButton Command1
Caption = “5“
Height = 615
Index = 5
Left = 1200
TabIndex = 6
Top = 1680
Width = 615
End
Begin VB.CommandButton Command1
Caption = “4“
Height = 615
Index = 4
Left = 480
TabIndex = 5
Top = 1680
Width = 615
End
Begin VB.CommandButton Command1
Caption = “3“
Height = 615
Index = 3
Left = 1920
TabIndex = 4
Top = 2400
Width = 615
End
Begin VB.CommandButton Command1
Caption = “2“
Height = 615
Index = 2
Left = 1200
TabIndex = 3
Top = 2400
Width = 615
End
Begin VB.CommandButton Command1
Caption = “1“
Height = 615
Index = 1
Left = 480
TabIndex = 2
Top = 2400
Width = 615
End
Begin VB.CommandButton Command1
Caption = “0“
Height = 615
Index = 0
Left = 480
TabIndex = 1
Top = 3120
Width = 615
End
Begin VB.TextBox Text1
Height = 285
Left = 480
TabIndex = 0
Top = 240
Width = 3495
End
End
Attribute VB_Name = “Form1“
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim mem1 As Long, mem2 As Long, mem3 As Long
Dim calc As Integer
Private Sub Command1_Click(Index As Integer)
Text1.Text = Text1.Text & Index
End Sub
Private Sub Command2_Click(Index As Integer)
calc = Index
mem1 = Text1.Text: Text1.Text = ““
End Sub
Private Sub Command3_Click(Index As Integer)
Select Case Index
Case 0
Text1.Text = ““
mem1 = 0: mem2 = 0: mem3 = 0
Case 1
Text1.Text = Text1.Text & “.“
Case 2
mem2 = Text1.Text: Text1.Text = ““
Select Case calc
Case 0 ’+
mem3 = mem1 + mem2
Case 1 ’-
mem3 = mem1 - mem2
Case 2 ’*
mem3 = mem1 * mem2
Case 3 ’/
mem3 = mem1 / mem2
End Select
Text1.Text = mem3
End Select
End Sub
用VB编写简单计算器程序
以下是关键代码:
Rem 判断文本框输入是否正确,可添加在“计算”按钮代码中If IsNumeric(Text1.Text) = False Then ’判断是否是数字
Msgbox “错误“,16,“错误提示“
Text1.SetFocus
End If
If IsNumeric(Text2.Text) = False Then
Msgbox “错误“,16,“错误提示“
Text2.SetFocus
End If
Rem 计算,可添加在“计算”按钮代码中
Select Case Text3.Text
Case “+“
Text4.text = Val(Text1.Text) + Val(Text2.Text)
Case “-“
Text4.text = Val(Text1.Text) - Val(Text2.Text)
Case “*“
Text4.text = Val(Text1.Text) * Val(Text2.Text)
Case “/“
Text4.text = Val(Text1.Text) / Val(Text2.Text)
Case Else
Msgbox “错误“,16,“错误提示“
Text3.SetFocus
End Select
Rem 结束,可添加在“结束”按钮代码中
End
希望能对你有帮助
如何用VB编写最简单的计算器程序
打开VB新建一个EXE;画上4个按钮控件表示加减乘除,再画上3个文本控件表示两个数与和数,再画一个标签控件加入“=”号。当然,界面可以自己发挥;画好之后,就可以把控件进行命名,注意是属性栏的Caption项命名,以便运行的时候方便识别;现在可以进行最重要的代码编写啦。双击对应的按钮控件,进入代码编写界面。Text3.Text = Val(Text1.Text) + Val(Text2.Text),其实代码很简单,就这一句只要更换把‘+’分别换成‘-’‘*’‘/’即可;现在可以点击运行或者F5键运行,即可得到第一个简单的计算器程序。
VB设计一个简易计算器
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 & Trim(Str(Index))
End If
Text1.Text = StrNum1
Case 10
If Not PointFlag Then
If FirstNum = True Then
StrNum1 = “0.“
FirstNum = False
Else
StrNum1 = StrNum1 + “.“
End If
PointFlag = True
Text1.Text = StrNum1
End If
Case 11
If Not SignFlag Then
StrNum1 = Text1.Text
Equal = Val(StrNum1)
Else
Call Run
End If
FirstNum = True
PointFlag = False
SignFlag = False
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 Else
Call cleardata
End Select
End Sub
怎样用vb编程一部简单的计算器呢
1、打开VB新建一个EXE。
2、如图画上4个按钮控件表示加减乘除,再画上3个文本控件表示两个数与和数,再画一个标签控件加入“=”号。当然,界面可以自己发挥。
3、画好之后,就可以把控件进行命名,注意是属性栏的Caption项命名,以便运行的时候方便识别。
4、好的,现在可以进行最重要的代码编写啦。双击对应的按钮控件,进入代码编写界面。Text3.Text = Val(Text1.Text) + Val(Text2.Text),其实代码很简单,就这一句只要更换把‘+’分别换成‘-’‘*’‘/’即可。
5、好了,现在可以点击运行或者F5键运行,即可得到第一个简单的计算器程序。
用VB编程序设计一个简易的计算器 要求如下:

更多文章:
majority of(the majority of 和 a majority of的区别以及用法例句)
2026年10月11日 07:40
another time(another time和other time的区别)
2026年10月11日 05:00








