VB问题 简单计算器 求助 求助
参考Windows附件的计算器,在VB中做一个简单计算器,可以实现简单的四则运算,包括优先级(例如可以计算1+2*3),参考界面如下图所示。(80分)要求:1、将0~9的...
参考Windows附件的计算器,在VB中做一个简单计算器,可以实现简单的四则运算,包括优先级(例如可以计算1+2*3),参考界面如下图所示。(80分)
要求:
1、将0~9的数字按钮做成控件数组
2、除可以用鼠标单击”=”按钮进行计算,还能实现当使用键盘,按下”=”号键也能完成输入式子的计算。
完成此功能,需要写窗体的键盘KeyPress事件(为使窗体能响应KeyPress事件,请设置窗体的KeyPreview属性为True) 展开
要求:
1、将0~9的数字按钮做成控件数组
2、除可以用鼠标单击”=”按钮进行计算,还能实现当使用键盘,按下”=”号键也能完成输入式子的计算。
完成此功能,需要写窗体的键盘KeyPress事件(为使窗体能响应KeyPress事件,请设置窗体的KeyPreview属性为True) 展开
1个回答
展开全部
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
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 广告
2023-08-25 广告
作为广州中仪测绘科技有限公司的工作人员,我回答如下:使用工程计算器需要遵循一定的步骤和技巧。首先,打开计算器后,根据需要设置计算器的模式,例如科学模式或程序员模式。然后,根据所进行的工程计算需求,输入相应的数值和运算符号。例如,在计算土方量...
点击进入详情页
本回答由广州中仪测绘提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询