高手们,界面就是这样,我要具体的说明,如何写代码,不要一下子堆一堆不知道往哪里写,小弟谢谢了!
用VB编写计算器
- 提问者网友:欲劫无渡
- 2021-04-29 07:33
- 五星知识达人网友:迷人又混蛋
- 2021-04-29 07:47
Dim x As String '定义一个全局变量x作为中间变量暂存数字用
Private Sub Command1_Click() '按下数字1
Text1.Text = Text1.Text & "1"
End Sub
Private Sub Command10_Click() '按下数字6
Text1.Text = Text1.Text & "6"
End Sub
Private Sub Command11_Click() '按下数字9
Text1.Text = Text1.Text & "9"
End Sub
Private Sub Command12_Click()
If Label1.Caption = "+" Then '执行加法运算
Text1.Text = Trim(Str(Val(x) + Val(Text1.Text)))
End If
If Label1.Caption = "-" Then '执行减法运算
Text1.Text = Trim(Str(Val(x) - Val(Text1.Text)))
End If
If Label1.Caption = "*" Then '执行乘法运算
Text1.Text = Trim(Str(Val(x) * Val(Text1.Text)))
End If
If Label1.Caption = "/" Then '执行除法运算
If Val(Text1.Text) = 0 Then
Text1.Text = "除数不能为零"
Else
If -1 < Val(x) / Val(Text1.Text) And Val(x) / Val(Text1.Text) < 0 Then '在-1~1之间的在小数点前面补上0
Text1.Text = Trim("-0" & Trim(Str(Abs(Val(x) / Val(Text1.Text)))))
Else
If 0 < Val(x) / Val(Text1.Text) And Val(x) / Val(Text1.Text) < 1 Then
Text1.Text = Trim("0" & Trim(Str(Val(x) / Val(Text1.Text))))
Else
Text1.Text = Trim(Str(Val(x) / Val(Text1.Text)))
End If
End If
End If
End If
End Sub
Private Sub Command13_Click() '按下加法按钮
x = Text1.Text
Text1.Text = ""
Label1.Caption = "+"
End Sub
Private Sub Command14_Click() '按下减法按钮
x = Text1.Text
Text1.Text = ""
Label1.Caption = "-"
End Sub
Private Sub Command15_Click() '按下乘法按钮
x = Text1.Text
Text1.Text = ""
Label1.Caption = "*"
End Sub
Private Sub Command16_Click() '按下除法按钮
x = Text1.Text
Text1.Text = ""
Label1.Caption = "/"
End Sub
Private Sub Command17_Click() '按下清除按钮
Text1.Text = ""
Text1.SetFocus
End Sub
Private Sub Command18_Click() '按下退出按钮
Unload Me
End Sub
Private Sub Command2_Click() '按下数字4
Text1.Text = Text1.Text & "4"
End Sub
Private Sub Command3_Click() '按下数字7
Text1.Text = Text1.Text & "7"
End Sub
Private Sub Command4_Click() ' 按下数字0
Text1.Text = Text1.Text & "0"
End Sub
Private Sub Command5_Click() '按下数字2
Text1.Text = Text1.Text & "2"
End Sub
Private Sub Command6_Click() '按下数字5
Text1.Text = Text1.Text & "5"
End Sub
Private Sub Command7_Click() '按下数字8
Text1.Text = Text1.Text & "8"
End Sub
Private Sub Command8_Click() '按下小数点
Text1.Text = Text1.Text & "."
End Sub
Private Sub Command9_Click() '按下数字3
Text1.Text = Text1.Text & "3"
End Sub
Private Sub Form_Load()
Label1.Visible = False '用来暂存运算符号用
End Sub