用VB编计算器的问题

来源:百度知道 编辑:UC知道 时间:2024/05/10 18:00:12
Dim x As Double
Dim oper As Integer
Dim point1 As Boolean

Private Sub Command1_Click(Index As Integer)
Text1 = Text1.Text + Command1(Index).Caption

End Sub

Private Sub Command2_Click(Index As Integer)
x = Val(Text1.Text)
Text1 = ""
oper = Index
End Sub

Private Sub Command3_Click()
Select Case oper
Case 0
Text1 = x + Val(Text1)
Case 1
Text1 = x - Val(Text1)
Case 2
Text1 = x * Val(Text1)
Case 0
Text1 = x / Val(Text1)
End Select
End Sub

Private Sub Command4_Click()
If point1 = False Then
Text1 = Text1.Text + Command4.Caption
point1 = True
End If
End Sub

Private Sub Command5_Click()
x = 0
oper = 0
Text1.Text = ""
End Sub

这是我的程序,可是在我输入等号后再进行第二次运算时新输入的数就直接出现在结果后,比如我已算出7+8=15,我想再继续运算输入1时,就出现了151,怎么办啊?

你的代码没有设计运算后的模式,所以只能再运算后,按command5来CE计算器,然后重新计算

你可以修改代码:

'全局添加一行:
Dim ys As Boolean

'然后:
Private Sub Form_Load()
ys = False
End Sub

'再然后修改Command1的代码:
Private Sub Command1_Click(Index As Integer)
If ys = False Then
Text1 = Text1.Text + Command1(Index).Caption
Else
Text1 = ""
Text1 = Command1(Index).Caption
ys = False
End If
End Sub

'修改command3的代码:
Private Sub Command3_Click()
Select Case oper
Case 0
Text1 = x + Val(Text1)
Case 1
Text1 = x - Val(Text1)
Case 2
Text1 = x * Val(Text1)
Case 3
Text1 = x / Val(Text1)
End Select
ys = True
End Sub

'修改command5的代码:
Private Sub Command5_Click()
x = 0
oper = 0
Text1.Text = ""
ys = False
End Sub

时间紧迫,告诉你方法,自己试用,希望对你有用.

将text1.text的值,每次都传给text2.text(此text属性为不可见)