VB问题 简单计算器 求助 求助

参考Windows附件的计算器,在VB中做一个简单计算器,可以实现简单的四则运算,包括优先级(例如可以计算1+2*3),参考界面如下图所示。(80分)要求:1、将0~9的... 参考Windows附件的计算器,在VB中做一个简单计算器,可以实现简单的四则运算,包括优先级(例如可以计算1+2*3),参考界面如下图所示。(80分)

要求:
1、将0~9的数字按钮做成控件数组
2、除可以用鼠标单击”=”按钮进行计算,还能实现当使用键盘,按下”=”号键也能完成输入式子的计算。
完成此功能,需要写窗体的键盘KeyPress事件(为使窗体能响应KeyPress事件,请设置窗体的KeyPreview属性为True)
展开
 我来答
bd5057
2015-04-28 · TA获得超过879个赞
知道大有可为答主
回答量:1242
采纳率:100%
帮助的人:1093万
展开全部
Private Sub Command1_Click(Index As Integer) '数字键
Text1.Text = Text1.Text & Command1(Index).Caption
End Sub

Private Sub Command2_Click() '等号=
Text1.Text = Text1.Text & "=" & CallVal(Text1.Text)
End Sub

Private Sub Command3_Click() '除法/
Text1.Text = Text1.Text & "/"
End Sub

Private Sub Command4_Click() '乘法*
Text1.Text = Text1.Text & "*"
End Sub

Private Sub Command5_Click() '减法-
Text1.Text = Text1.Text & "-"
End Sub

Private Sub Command6_Click() '加法+
Text1.Text = Text1.Text & "+"
End Sub

Private Sub Command7_Click() '清空
Text1.Text = ""
End Sub

Private Sub Command8_Click() '退格<
If Text1.Text = "" Then Exit Sub
Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1)
End Sub

Private Sub Form_KeyPress(KeyAscii As Integer) '窗体的键盘按下事件
If Chr(KeyAscii) = "=" Then
KeyAscii = 0
Text1.Text = Text1.Text & "=" & CallVal(Text1.Text)
End If
End Sub

Private Sub Form_Load()
Dim i As Integer
Dim xTop As Integer
xTop = 3200
Command1(0).Top = xTop
Command1(0).Height = 375
Command1(0).Left = 240
Command1(0).Width = 1200
Command1(0).Caption = "0"
Command2.Top = xTop
Command2.Height = 375
Command2.Left = 1680
Command2.Width = 1200
Command2.Caption = "="

For i = 1 To 9
Load Command1(i)
If i Mod 3 = 1 Then
Command1(i).Left = Command1(0).Left
Command1(i).Top = Command1(i - 1).Top - 240 - 375
Else
Command1(i).Left = Command1(i - 1).Left + 720
Command1(i).Top = Command1(i - 1).Top
End If
Command1(i).Width = 480
Command1(i).Caption = i
Command1(i).Visible = True
Next
Command3.Top = xTop - 375 - 240
Command3.Height = 375
Command3.Left = 2400
Command3.Width = 480
Command3.Caption = "/"
Command4.Top = xTop - 375 * 2 - 240 * 2
Command4.Height = 375
Command4.Left = 2400
Command4.Width = 480
Command4.Caption = "×"
Command5.Top = xTop - 375 * 3 - 240 * 3
Command5.Height = 375
Command5.Left = 2400
Command5.Width = 480
Command5.Caption = "-"
Command6.Top = xTop - 375 * 4 - 240 * 4
Command6.Height = 375
Command6.Left = 2400
Command6.Width = 480
Command6.Caption = "+"
Command7.Top = xTop - 375 * 4 - 240 * 4
Command7.Height = 375
Command7.Left = 960
Command7.Width = 1200
Command7.Caption = "清空"
Command8.Top = xTop - 375 * 4 - 240 * 4
Command8.Height = 375
Command8.Left = 240
Command8.Width = 480
Command8.Caption = "<"
Text1.Text = ""
End Sub
Private Function CallVal(ByVal inputStr As String) As String '使用ScriptControl计算的函数

Dim SC
On Error GoTo er
Set SC = CreateObject("ScriptControl")
SC.Language = "VBScript"
CallVal = SC.Eval(inputStr)
Exit Function
er:
CallVal = "错误"
End Function
意法半导体(中国)投资有限公司
2023-08-25 广告
C51单片机是一种基于8051微处理器内核的微控制器,常用于嵌入式系统开发。如果您想学习C51单片机,可以遵循以下步骤:1. 学习微控制器的基本原理和C语言编程基础,了解C51单片机的特点和引脚定义。2. 获取一块C51单片机开发板,熟悉单... 点击进入详情页
本回答由意法半导体(中国)投资有限公司提供
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式