VB制作加减法计算器

如题所述

 

 

 

楼猪在界面上放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

温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-11-13

 

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

 

 

第2个回答  2012-11-13
可以做一个完整的计算器

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网