• 主页

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

zyx_1984 2008-01-20 09:32:38

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
...全文
167 点赞 收藏 9

9 条回复

sudeping_ok 2008-02-21

Private Sub Command1_Click()
Shell "calc.exe"
End Sub

zyx_1984 2008-02-19

Cauly123 2008-02-16

DaiLM 2008-02-15

10个数字button和4个运算符button都是数组型的，代码可以写在一起，然后通过index进行判断

'数字键
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

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
``````

caofusheng 2008-01-20

luojian909 2008-01-20

VB基础类

7453

VB 基础类