如题所述
楼猪在界面上放3个文本框控件,一个组合框控件,一个按钮控件。
按钮名称是command1,文本框名称分别是text1和text2和text3,组合框名称是combo1,代码如下:
Private Sub Form_Load()
Combo1.Text=""
Text1=""
Text2=""
Text3=""
Combo1.AddItem "+"
Combo1.AddItem "-"
Combo1.AddItem "*"
Combo1.AddItem "/"
Command1.Caption="计算"
End Sub
Private Sub Command1_Click()
If Combo1.Text = "+" Then
Text3.Text = Val(Text1.Text) + Val(Text2.Text)
ElseIf Combo1.Text = "-" Then
Text3.Text = Val(Text1.Text) - Val(Text2.Text)
ElseIf Combo1.Text = "*" Then
Text3.Text = Val(Text1.Text) * Val(Text2.Text)
ElseIf Combo1.Text = "/" Then
If Text3.Text = "0" Then
MsgBox "除数不能为0!"
Else
Text3.Text = Val(Text1.Text) / Val(Text2.Text)
End If
End If
End Sub
Option Explicit
Private Sub Combo1_Change()
End Sub
Private Sub Command1_Click()
If IsNumeric(Text1.Text) And IsNumeric(Text2.Text) Then
Select Case Combo1.Text
Case "+"
Text3.Text = CLng(Text1.Text) + CLng(Text2.Text)
Case "-"
Text3.Text = CLng(Text1.Text) - CLng(Text2.Text)
Case "×"
Text3.Text = CLng(Text1.Text) * CLng(Text2.Text)
Case "÷"
If CLng(Text2.Text) <> 0 Then Text3.Text = CLng(Text1.Text) / CLng(Text2.Text) Else MsgBox "出数不能为0", vbOKOnly, "提示"
Case Else
MsgBox "请选择运算方式", vbOKOnly, "提示"
End Select
Else
MsgBox "请输入数字", vbOKOnly, "提示"
End If
End Sub
Private Sub Form_Load()
Combo1.AddItem "+"
Combo1.AddItem "-"
Combo1.AddItem "×"
Combo1.AddItem "÷"
End Sub