End If
Runsign = Index - 11 ‘储存键入的运算符
里面运算的部分我用了一个过程,是因为在按下等号时还会调用到运算过程,这样可以减少代码的输入,也使程序便于浏览。建立过程(Procedure),可以在General部分直接输入,也可以通过菜单里的“Add Procedure”添加。Run的代码如下: Sub Run()
Num1 = Val(StrNum2) Num2 = Val(StrNum1) Select Case Runsign Case 1’加
equal = Num1 + Num2 Case 2’减
equal = Num1 - Num2 Case 3’乘
equal = Num1 * Num2 Case 4’除
equal = Num1 / Num2 End Select
StrNum2 = Str(equal) StrNum1 = StrNum2 Text1.Text = StrNum2 End Sub
下面是其余的代码:(在Command1_Click事件里,接着写) Case 11
If Not SignFlag Then
20
Text1.Text = StrNum1 equal = Val(StrNum1) FirstNum = True PointFlag = False Else
Call Run
SignFlag = False End If
Case Else’清除按钮 Call ClearData
里面又用到了一个ClearData过程,实际上就是Form_Load里的变量初赋值,代码如下: Sub ClearData() Num1 = 0 Num2 = 0 StrNum1 = \StrNum2 = \FirstNum = True PointFlag = False Runsign = 0
SignFlag = False Text1.Text = \ End Sub
21
这样完整的程序就完成了,本章介绍了VB的第一个控件――按钮,并且举例,希望你继续看下一章――菜单。 下面给出完整的程序代码: Dim Num1, Num2 As Single Dim StrNum1, StrNum2 As String
Dim FirstNum As Boolean '判断是否是数字开头 Dim PointFlag As Boolean '判断是否已有小数点 Dim Runsign As Integer '储存运算符号
Dim SignFlag As Boolean '判断是否已有运算符号
Sub ClearData() Num1 = 0 Num2 = 0 StrNum1 = \ StrNum2 = \ FirstNum = True
22
PointFlag = False Runsign = 0 SignFlag = False Text1.Text = \ End Sub Sub Run()
Num1 = Val(StrNum2) Num2 = Val(StrNum1) Select Case Runsign Case 1
equal = Num1 + Num2 Case 2
equal = Num1 - Num2 Case 3
equal = Num1 * Num2 Case 4
equal = Num1 / Num2 End Select
StrNum2 = Str(equal)StrNum1 = StrNum2 Text1.Text = StrNum2End Sub
23
Private Sub about_Click()
MsgBox \周晓阳。1998/7/27\vbDefaultButton1, \教程第四章范例\ End Sub
Private Sub Command1_Click(Index As Integer) Select Case Index Case 0 To 9 If FirstNum Then StrNum1 = Str(Index) FirstNum = False Else
StrNum1 = StrNum1 + Str(Index) End If
Text1.Text = StrNum1 Case 10
If Not PointFlag Then If FirstNum Then StrNum1 = \ FirstNum = False Else
StrNum1 = StrNum1 + \ End If Else
24