VB编程入门
编一个计算器要求:只有一个TEXT(文本框)要有+-*/还要有=
追问:哈哈 真是非常非常感谢啦 帮了大大的忙了
追问:哈哈 真是非常非常感谢啦 帮了大大的忙了
答案:一个简单的计算器:代码
'定义变量
Dim optiontag, a1, k
'数字0
Private Sub cmd0_Click()
txt1 = txt1 & 0
End Sub
'数字1
Private Sub cmd1_Click()
txt1 = txt1 & 1
End Sub
'数字2
Private Sub cmd2_Click()
txt1 = txt1 & 2
End Sub
'数字3
Private Sub cmd3_Click()
txt1 = txt1 & 3
End Sub
'数字4
Private Sub cmd4_Click()
txt1 = txt1 & 4
End Sub
'数字5
Private Sub cmd5_Click()
txt1 = txt1 & 5
End Sub
'数字6
Private Sub cmd6_Click()
txt1 = txt1 & 6
End Sub
'数字7
Private Sub cmd7_Click()
txt1 = txt1 & 7
End Sub
'数字8
Private Sub cmd8_Click()
txt1 = txt1 & 8
End Sub
'数字9
Private Sub cmd9_Click()
txt1 = txt1 & 9
End Sub
'+号
Private Sub cmdadd_Click()
a1 = txt1
txt1 = ""
optiontag = 1
End Sub
'*号
Private Sub cmdche_Click()
a1 = txt1
txt1 = ""
optiontag = 3
End Sub
'/号
Private Sub cmdchu_Click()
a1 = txt1
txt1 = ""
optiontag = 4
End Sub
'清除
Private Sub cmdclear_Click()
txt1 = ""
End Sub
'结果
Private Sub cmdjie_Click()
'判断文本框是否有数字输入
If txt1 = "" Then
k = MsgBox("请输入数据!", vbOKOnly, "信息提示")
End If
'计算部分
Select Case optiontag
Case 1
txt1.Text = Val(a1) + Val(txt1.Text)
Case 2
txt1.Text = Val(a1) - Val(txt1.Text)
Case 3
txt1.Text = Val(a1) * Val(txt1.Text)
Case 4
'判断分母不能为0
If txt1.Text = "0" Then
k = MsgBox("分母不能为0!", vbOKOnly, "信息提示")
txt1.SetFocus
Else
txt1.Text = Val(a1) / Val(txt1.Text)
End If
End Select
End Sub
'-号
Private Sub cmdsub_Click()
a1 = txt1
txt1 = ""
optiontag = 2
End Sub