VB6计算器:在屏幕上显示操作员标志

时间:2012-01-24 17:04:52

标签: vb6

我刚开始自学VB6。我创建了一个简单的计算器,我希望它能在屏幕上显示“操作符”。

例如,如果我按“1”,然后按“加号”,最后按“8”,我希望计算器显示“1 + 8”。当按下“相等”符号时,计算器应显示“1 + 8 = 9”。

以下是我制作的非常菜鸟代码:

    Dim formula As String
    Dim itemOne As Integer
    Dim itemTwo As Integer

    Private Sub btn1_Click()
    txtboxScreen.Text = txtboxScreen.Text & 1
    End Sub

    Private Sub btn2_Click()
    txtboxScreen.Text = txtboxScreen.Text & 2
    End Sub

    Private Sub btn3_Click()
    txtboxScreen.Text = txtboxScreen.Text & 3
    End Sub

    Private Sub btn4_Click()
    txtboxScreen.Text = txtboxScreen.Text & 4
    End Sub

    Private Sub btn5_Click()
    txtboxScreen.Text = txtboxScreen.Text & 5
    End Sub

    Private Sub btn6_Click()
    txtboxScreen.Text = txtboxScreen.Text & 6
    End Sub

    Private Sub btn7_Click()
    txtboxScreen.Text = txtboxScreen.Text & 7
    End Sub

    Private Sub btn8_Click()
    txtboxScreen.Text = txtboxScreen.Text & 8
    End Sub

    Private Sub btn9_Click()
    txtboxScreen.Text = txtboxScreen.Text & 9
    End Sub

    Private Sub btnDivide_Click()
    itemOne = txtboxScreen.Text
    txtboxScreen.Text = ""
    formula = "/"
    End Sub

    Private Sub btnEqual_Click()
    itemTwo = txtboxScreen.Text
    If formula = "+" Then
    txtboxScreen.Text = itemOne + itemTwo
    ElseIf formula = "-" Then
    txtboxScreen.Text = itemOne - itemTwo
    ElseIf formula = "*" Then
    txtboxScreen.Text = itemOne * itemTwo
    ElseIf formula = "/" Then
    txtboxScreen.Text = itemOne / itemTwo
    End If
    End Sub

    Private Sub btnMinus_Click()
    itemOne = txtboxScreen.Text
    txtboxScreen.Text = ""
    formula = "-"
    End Sub

    Private Sub btnPlus_Click()
    itemOne = txtboxScreen.Text
    txtboxScreen.Text = ""
    formula = "+"
    End Sub

    Private Sub btnTimes_Click()
    itemOne = txtboxScreen.Text
    txtboxScreen.Text = ""
    formula = "*"
    End Sub

    Private Sub btnZero_Click()
    txtboxScreen.Text = txtboxScreen.Text & 0
    End Sub

3 个答案:

答案 0 :(得分:3)

您可能需要考虑为数字按钮使用control array。这将大大简化您在此实例中的代码,尤其是对于更复杂的项目:

    Private formula As String
    Private itemOne As Integer
    Private itemTwo As Integer

    Private Sub btnNumbers_Click(Index As Integer)
          txtboxScreen.Text = txtboxScreen.Text & Index
    End Sub

''Remainder of your code goes here

此外,当您在表单的声明部分声明变量时,您应该使用Private而不是Dim。

答案 1 :(得分:1)

我认为您希望将按下的按钮的操作员符号与button_click事件中文本框中的值相连接。

类似的东西:

Private Sub btnPlus_Click()
    txtboxScreen.Text = txtboxScreen.Text & " + "
End Sub

在等号按钮上,您想要评估表达式

Private Sub btnEqual_Click()
    txtboxScreen.Text = txtboxScreen.Text & " = " & Eval(txtboxScreen.Text)
End Sub

使用Eval()进行评估并不是一个强大的解决方案,但它是实现该功能的一种简单方法。

答案 2 :(得分:1)

您应该分别保存您的第一个号码,第二个号码和运营商(您称之为“公式”)并分别处理设置文本框的文本。这是一种方法:

Dim formula As String
Dim itemOne As String 'This time this is string
Dim itemTwo As String 'This time this is string

Dim currentItem As String 'Will hold the current number being entered
Dim Result As String 'This is string, too.

所有按钮的代码如下:

Private Sub btn1_Click()
    currentItem = currentItem & "1"
    UpdateText()
End Sub

操作员按钮:

Private Sub btnPlus_Click()
    itemOne = currentItem
    formula = "+"
    UpdateText()
End Sub

等于按钮:

Private Sub btnEqual_Click()
    itemTwo = currentItem
    If formula = "+" Then
        'Str is optional, but Val's are necessary since
        'itemOne and itemTwo are strings.
        Result = Str( Val(itemOne) + Val(itemTwo) )
    ElseIf ...
        .
        .
        .
    End If
    UpdateText()
End Sub

那么,注意到每个子程序结束时对UpdateText()的调用?这是:

Private Sub UpdateText()
    txtboxScreen.Text = itemOne & formula & itemTwo
    'If result is not empty, we will add the '=' part too
    If Result <> "" Then
        txtboxScreen.Text = txtboxScreen.Text & "=" & Result
    End If
End Sub

您可能还对AC/ON键感兴趣,该键将所有变量设置为""

方法不是那么整洁,但如果没有表达式评估器,这是最好的办法。表达式计算器可以按原样计算整个公式。 Eval就是这样一个函数,您可以在网上找到它的一些实现。