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
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能需要考虑使用控制数组< /a> 为您的数字按钮。在这种情况下,这将极大地简化您的代码,尤其是对于更复杂的项目:
此外,当您在表单的“声明”部分中声明变量时,您应该使用 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:
Also, when you are declaring variables in the Declaration section of the form you should use Private instead of Dim.
我认为您希望将按下的按钮的运算符号与button_click事件的文本框中的值连接起来。
类似于:
在 equal 按钮上,您想要评估表达式
,使用 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:
and on equal button, you'd like to evaluate the expression
evaluating using Eval() is not a robust solution, but it's a simple way to achive that functionality.
您应该分别保存第一个数字、第二个数字和运算符(您将其称为“公式”),并分别处理设置文本框的文本。这是一种方法:
所有按钮都将具有如下代码:
运算符按钮:
和等于按钮:
好吧,注意到每个子过程末尾对
UpdateText()
的调用了吗?如下:您可能还对
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:
All your buttons will have code like:
The operator buttons:
And the Equals button:
Well, noticed the call to
UpdateText()
at the end of every subprocedure? Here it is: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.