VB6计算器:在屏幕上显示操作符

发布于 2024-12-28 23:53:23 字数 2045 浏览 0 评论 0原文

我刚刚开始自学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

I just started learning VB6 on my own. I have created a simple calculator and I would like it to display the "operator" on the screen.

For example, if I press "1", followed by "plus sign", then finally "8", I would like the calculator to show "1 + 8". And when the "equal" sign is pressed, the calculator should show "1 + 8 = 9".

Below is a very noob code I made:

    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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜雨飘雪 2025-01-04 23:53:23

您可能需要考虑使用控制数组< /a> 为您的数字按钮。在这种情况下,这将极大地简化您的代码,尤其是对于更复杂的项目:

    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。

You may want to think about using a control array for you number buttons. This will vastly simplify your code in this instance and especially for more complex projects:

    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

Also, when you are declaring variables in the Declaration section of the form you should use Private instead of Dim.

找回味觉 2025-01-04 23:53:23

我认为您希望将按下的按钮的运算符号与button_click事件的文本框中的值连接起来。

类似于:

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

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

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

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

I think you would like to concatenate the operator sign of the pressed button with the value you had in the textbox on button_click event.

something like:

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

and on equal button, you'd like to evaluate the expression

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

evaluating using Eval() is not a robust solution, but it's a simple way to achive that functionality.

北方的巷 2025-01-04 23:53:23

您应该分别保存第一个数字、第二个数字和运算符(您将其称为“公式”),并分别处理设置文本框的文本。这是一种方法:

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 就是这样一个函数,你可以在网上找到它的一些实现。

You should save your first number, second number and operator (you called it "formula") separately and handle setting the text of the text box separately. Here is one way to do it:

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.

All your buttons will have code like:

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

The operator buttons:

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

And the Equals button:

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

Well, noticed the call to UpdateText() at the end of every subprocedure? Here it is:

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

You might also be interested in an AC/ON key which sets all the variables to "".

The method was not so neat, but it's the best thing you can do without an expression evaluator. An expression evaluator can calculate the entire formula as it is. Eval is such a function and you can find some implementations of it on the net.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文