小弟刚学vb,编个计算器程序!可是有个问题!希望各位大侠帮忙!

zyx_1984 2008-01-20 09:32:38
数字计算完以后,在点1的按钮,就接着连着计算后的数!这怎么解决啊!?
以下是我部分代码
Dim a As Integer
Dim b As Integer
Dim c As String
Dim x As Integer

Private Sub a0_Click(Index As Integer)
If x = 1 Then
Text1.Text = "0"
If Text1.Text = "" Then
Text1.Text = Text1.Text & "0"
Exit Sub
End If
End If
Text1.Text = Text1.Text & "0"
End Sub

Private Sub a1_Click(Index As Integer)
If x = 1 Then
Text1.Text = ""
End If
If x = 1 And Text1.Text = "" Then
Text1.Text = Text1.Text & "1"
End If
Text1.Text = Text1.Text & "1"
End Sub

Private Sub a2_Click(Index As Integer)
If x = 1 Then
Text1.Text = "2"
End If
If Text1.Text <> "" Then
Text1.Text = " "
Text1.Text = Text1.Text & "2"
End If

Text1.Text = Text1.Text & "2"
End Sub

Private Sub a3_Click(Index As Integer)
x = True
Text1.Text = Text1.Text & "3"
End Sub

Private Sub a4_Click(Index As Integer)
Text1.Text = Text1.Text & "4"
End Sub

Private Sub a5_Click(Index As Integer)

Text1.Text = Text1.Text & "5"
End Sub

Private Sub a6_Click(Index As Integer)

Text1.Text = Text1.Text & "6"
End Sub

Private Sub a7_Click(Index As Integer)

Text1.Text = Text1.Text & "7"
End Sub

Private Sub a8_Click(Index As Integer)

Text1.Text = Text1.Text & "8"
End Sub

Private Sub a9_Click(Index As Integer)

Text1.Text = Text1.Text & "9"
End Sub

Private Sub b1_Click(Index As Integer)
a = Val(Text1.Text)
b = 1
Text1.Text = ""
End Sub

Private Sub b2_Click(Index As Integer)
a = Val(Text1.Text)
b = 2
Text1.Text = ""

End Sub

Private Sub b3_Click(Index As Integer)
a = Val(Text1.Text)
b = 3
Text1.Text = ""
End Sub

Private Sub b4_Click(Index As Integer)
a = Val(Text1.Text)
b = 4
Text1.Text = ""
End Sub

Private Sub b5_Click()
Select Case b
Case 1
c = Str(a + Val(Text1.Text))
x = 1
Case 2
c = Str(a - Val(Text1.Text))
Case 3
c = Str(a * Val(Text1.Text))
Case 4
If Text1.Text = "0" Then
MsgBox "除数不能为零!"
Else
Text1.Text = Str(a / Val(Text1.Text))
End If
Case Else
Text1.Text = ""
End Select
Text1.Text = c
End Sub

Private Sub return_Click(Index As Integer)
Text1.Text = "0"
End Sub

Private Sub start_Click(Index As Integer)
End
End Sub


Private Sub Text1_Change()
Text1.Text = Val(Text1.Text)


End Sub
...全文
211 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
sudeping_ok 2008-02-21
  • 打赏
  • 举报
回复
那有这么麻烦,教你一个最简单的办法
Private Sub Command1_Click()
Shell "calc.exe"
End Sub
搞定,嘿嘿
zyx_1984 2008-02-19
  • 打赏
  • 举报
回复
我搞出来了!呵呵,谢谢个位
Cauly123 2008-02-16
  • 打赏
  • 举报
回复
用一个boolean来判断现在的输入框显示的是结果还是正在输入,如果是结果那么就在输入的时候把输入框先清了
DaiLM 2008-02-15
  • 打赏
  • 举报
回复
感觉怪怪的

10个数字button和4个运算符button都是数组型的,代码可以写在一起,然后通过index进行判断
不过好像button的名字都已经被改了

'数字键
Private Sub btnNumber_Click(Index As Integer)

Text1.Text = Text1.Text & Index
'抱歉,很久不用VB了,不知道这样写是不是可以

End Sub

'+ - * / 键
Private Sub btnOperate_Click(Index As Integer)
a = Val(Text1.Text)
b = Index
Text1.Text = ""
End Sub

还有小数点的输入,backspace和delete,最后是数字溢出的判断等等
其他的应该没什么了吧
zyx_1984 2008-01-24
  • 打赏
  • 举报
回复
搞明白了,谢谢
cike_1111 2008-01-21
  • 打赏
  • 举报
回复
=====cike例题=====
name = 4句代码的计算器
form = 3个text 1个command 4个option
设置:
1\3个text的值清空
2\4个option的标题分别为"+-*/" [option1=+ option2=- ...4=/]
3\command的标题为"="
4\位置排序: text1 下面 有4个 option 再下面是text2 再下面是 command 最下面是text3
效果:运行后可以是个简单的计算器...
==================
代码:

Private Sub Command1_Click()
If Option1 Then Text3 = Val(Text1) + Val(Text2)
If Option2 Then Text3 = Val(Text1) - Val(Text2)
If Option3 Then Text3 = Val(Text1) * Val(Text2)
If Option4 And Val(Text2) <> 0 Then Text3 = Val(Text1) / Val(Text2)
End Sub
波导终结者 2008-01-21
  • 打赏
  • 举报
回复
变量名控件名全部是abc……

按下1以后把text1.text=""便是了
caofusheng 2008-01-20
  • 打赏
  • 举报
回复
不明白楼主的意思
luojian909 2008-01-20
  • 打赏
  • 举报
回复
我也没看明白...

7,785

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧