求VB高手指点

来源:百度知道 编辑:UC知道 时间:2024/06/01 04:24:17
Private Sub Command1_Click()
If Text4 = "+" Or Text4 = "-" Or Text4 = "*" Or Text4 = "/" Then
Text3 = Text3 + "1"
Else: Text2 = Text2 + "1"
End If
End Sub

Private Sub Command10_Click()
If Text4 = "+" Or Text4 = "-" Or Text4 = "*" Or Text4 = "/" Then
Text3 = Text3 + "0"
Else: Text2 = Text2 + "0"
End If
End Sub

Private Sub Command11_Click()

End Sub

Private Sub Command12_Click()
Dim a As Long
Dim b As Long
Dim c As Long
a = Val(Text2.Text)
b = Val(Text3.Text)
If Text4 = "+" Then
c = a + b
ElseIf Text4 = "-" Then
c = a - b
ElseIf Text4 = "/" Then
If b = 0 Then
MsgBox "除数不能为0"
Else:
c = a / b
End If
Else
c = a * b
End If
Text1 = ""
T

定义一个 Boolean 类型的变量,用于记录“是否点过小数点按钮”。

然后:

If 是否点过小数点按钮 = False Then txtLED.text = txtLED.text + "."

文本框:txtLED 表示“计算器”的“显示”文本框。
因为我不知道楼主代码中 Text1、Text2……表示什么。所以用别的变量名代替。

还有,计算器中的数字键为什么不用数组控件来实现呢?

只要把数字键的 CommandButton 控件的 Name 属性写一样的就行了,然后就是 Index 属性。

例:
计算器的数字键
“1”就用 CommandButton 控件,Name = cmdNumBtn,Index = 1
“2”就用 CommandButton 控件,Name = cmdNumBtn,Index = 2
……

然后,代码就可以这样写:

Private Sum cmdNumBtn_Click(Index As Integer)
'你点几号数字键,Index 就等几。

txtLED.text = txtLED.text & CStr(Index)

End Sub

这样,十个 Sub 就可以写成 一个 Sub 了。

Private Sub Command11_Click()
text1.text=text1.text & "."
End Sub
你将小数点添加在什么地方,就上面的text1.text修改为控件名称.属性即可。
其他没有详细看。

因你是学习中的初步学者,就说明一下思路吧。

1) 当点击【.】Button的话 txtLED.text = txtLED.text + "."

2) 当点击【.】Button 运算时使用Double类型(Long)运算。