【例4-1】 编写程序,输入姓名、年龄和两门课的成绩,在窗体上打印姓名、年龄和两门课的总分,用InputBox函数输入数据
Private Sub Form_Click()
Title = "InputBox函数示例"
Msg1 = "请输入你的姓名"
Msg2 = "请输入你的年龄"
YourName = InputBox(Msg1,Title)
YourAge = InputBox(Msg2,Title)
Msg1 = "请输入第一门课的成绩"
Msg2 = "请输入第二门课的成绩"
Num1 = InputBox(Msg1,Title)
Num1 = Val(Num1)
Num2 = InputBox(Msg2,Title)
Num2 = Val(Num2)
Cls
Print "Name:";YourName; ","; "Age:";YourAge; ","; "Total:";Num1 + Num2
End Sub
【例4-2】输入圆的半径,求圆的面积。
Private Sub Form_Click()
Const Pi = 3.1416
Dim r As Double,a as Double
r = InputBox("请输入圆的半径")
a = Pi * r * r
MsgBox("经计算,圆的面积为:" & a )
End Sub
【例4-3】 输入圆的半径,求圆的面积。
Private Sub cmdCalcu_Click()
Const Pi = 3.1416
Dim r As Double,S As Double
r = Text1.text
s = Pi * r * r
Label2.Caption =,经计算,圆的面积为:” &s
 End Sub
【例4-4】 编写程序,实现连续打印“打印测试页1”和“打印测试页2”两张测试页:
Private Sub Form_Click()
Printer.Print "打印测试页" + Printer.Page
Printer.NewPage
Printer.Print "打印测试页" + Printer.Page
Printer.EndDoc
End Sub
【例4-5】 输入x的值,并打印出其绝对值。
Private Sub Command1_Click()
Dim x As Integer
x = Val(InputBox("请输入一个数"))
If x >= 0 Then Print "x的绝对值是:"; Str(x) Else x = -x,Print "x的绝对值是:"; Str(x)
End Sub
例4-6】 有以下函数:

要求输入x,并求出y的值。
Private Sub Form_Click()
Dim x As Single,y As Single
x=InputBox("请输入x的值")
If x>0 Then y=1 Else If x<0 Then y=-1 Else y=0
Print "x=";x,"y=";y
End Sub
【例4-7】输入一个分数,并判断其等级。
Private Sub Command1_Click()
a = InputBox(″请输入分数″)
If a < 60 Then
Print ″不及格″
ElseIf a < 80 Then
Print ″及格″
ElseIf a < 90 Then
Print ″良好″
ElseIf a < 100 Then
Print ″优秀″
ElseIf a = 100 Then
Print ″满分″
Else
Print ″不合适的分数″
End If
End sub
【例4-8】编写程序,验证MsgBox函数的功能。
 
Private Sub Form_Click()
Cls
Msgtitle$ = "数据检查"
Msg$ = "数据正确吗?"
Button = 3 + 48 + 0
a = MsgBox(Msg$,Button,Msgtitle$)
If a = 6 Then
Print "你按下来Yes按钮"
Else
If a = 2 Then
Print "你按下了Cancel按钮"
Else
Print "你按下了No按钮"
End If
End If
b = MsgBox("继续",vbAbortRetryIgnore + vbQuestion + vbDefaultButton1,"操作对话框")
If b = 4 Then
Print "重试(Retry)"
Else
If b = 3 Then
Print "终止(Abort)"
Else
Print "忽略(Ignore)"
End If
End If
End Sub
【例4-9】 某商店进行购物打折优惠活动促销,根据每位顾客一次性购物的消费额给予不同的折扣,具体方法如下:
(1) 购物1000元以上的九五折优惠,
(2) 购物2000元以上的九折优惠,
(3) 购物3000元以上的八五折优惠,
(4) 购物5000元以上的八折优惠。
请用Select Case语句编写程序,输入购物消费额,计算出优惠后应收款。
Private Sub Command1_Click()
Dim x As Single,y As Single
x = Val(InputBox("请输入x值:"))
Select Case x
Case Is < 1000
Print "不优惠"
y = x
Case Is < 2000
Print "九五折优惠"
y = 0.95 * x
Case Is < 3000
Print "九折优惠"
y = 0.9 * x
Case Is < 5000
Print "八五折优惠"
y = 0.85 * x
Case Is >= 5000
Print "八折优惠"
y = 0.8 * x
End Select
Print "应收款额为:"; y
End Sub
【例4-10】 求1到100的和。
Private Sub Command1_Click()
Dim i As Integer,sum As Integer
sum = 0
For i = 1 To 100
sum = sum + i
Next
Print sum
End Sub
【例4-11】 求Fibonacci数列的前40个数。这个数列有如下特点:前两个数为1,从第三个数开始,其值是前两个数的和,即:
F1=1 (n=1)
F2=1 (n=2)
Fn= Fn-1+ Fn-2 (n≥3)
Private Sub Command1_Click()
Dim i As Integer
Dim f1 As Long,f2 As Long,fn As Long
f1 = 1
f2 = 1
Print f1,
Print f2,
For i = 3 To 40 'f1,f2已知,从第三个数开始计算
fn = f1 + f2
f1 = f2
f2 = fn '更改f1,f2的值
Print fn,
If i Mod 4 = 0 Then Print '打印4个数后换行打印
Next
End Sub
【例4-12】用辗转相除法求两自然数m,n的最大公约数和最小公倍数。
Private Sub Form_Click()
n1 = InputBox("输入n1")
m1 = InputBox("输入m1")
If m1 > n1 Then ' 为了求最小公倍数,增加m,n变量 
 m = m1,n = n1 ' 使得m>n
Else
 m = n1,n = m1
End If
Do
 r = m Mod n
 m = n
 n = r
Loop while ( r <>0)
Print m1; ","; n1; "的最大公约数为"; m
Print "最小公倍数= ",m1 * n1 /m
End Sub
【例4-13】 验证“角谷猜想”。“角谷猜想”指出,对于一个自然数,若该数为偶数,则除以2;若该数为奇数,则乘以3并加1;将得到的数再重复按该规则运算,最终可得到1。现给定一个数n,试用程序来验证该过程。
Private Sub Form_Click()
n = InputBox(″请输入待验证的数″)
Print
Print n;
Do While True
If n Mod 2 = 0 Then
n = n / 2
Else
n = n * 3 + 1
End If
Print n;
If n=1 Then Exit Do
Loop
End Sub
【例4-14】 利用公式…,求π的近似值,直到最后一项的绝对值小于等于10-6为止。
Private Sub Command1_Click()
Dim n As Long
Dim pi As Single,t As Single,s As Single
pi = 0
s = 1
t = 1
n = 1
'如果数据项大于10-6继续循环,直到小于等于10-6时结束循环
While (Abs(t) > 0.00001)
pi = pi + t
n = n + 2 '分母的值每次加2
s = -s '模拟数据项符号的正负交替变化
t = s / n '计算数据项的值
Wend
pi = pi * 4
Print pi
End Sub
【例4-15】 通过键盘输入一个字符串,统计其中a,b,c出现的次数。
Private Sub Command1_Click()
Dim str As String,str1 As String
Dim i As Integer,x As Integer,y As Integer,z As Integer
i = 1
x = 0
y = 0
z = 0
str = InputBox("请输入一个字符串:")
While i <= Len(str)
str1 = Mid(str,i,1) '取得字符串中第i个字符
Select Case str1 '统计a,b,c出现的次数
Case "a"
x = x + 1
Case "b"
y = y + 1
Case "c"
z = z + 1
End Select
i = i + 1
Wend
Print "a出现的次数为:"; x
Print "b出现的次数为:"; y
Print "c出现的次数为:"; z
End Sub
【例4-16】 打印乘法九九表。
Private Sub Form_Click()
Dim I As Integer,J As Integer
For I = 1 to 9
Print
For J = 1 to I
 Print J ; ″*″ ; I ; ″=″ ; I*J,
Next J
Next I
End Sub
【例4-17】 将1角钱兑换成分币有多少种方案。
Private Sub Form_Click()
Dim I,J,K,S As Integer
S=0 '兑换方案数
For I = 0 to 10 '兑换1分个数
For J = 0 to 10 Step 2 '兑换2分个数
For K = 0 to 10 Step 5 '兑换5分个数
If I+ J + K = 10 Then S = S + 1
Next K
Next J
Next I
Print ″兑换方案有″ ; S ; ″种。″
End Sub
【例4-18】 利用单选按钮改变文字的颜色。
Private Sub Option1_Click()
Label1.ForeColor = RGB(255,0,0)
End Sub
Private Sub Option2_Click()
Label1.ForeColor = RGB(0,0,255)
End Sub
Private Sub Option3_Click()
Label1.ForeColor = RGB(0,255,0)
End Sub
Private Sub Option4_Click()
Label1.ForeColor = RGB(0,0,0)
End Sub
【例4-19】 在例4-18的基础上,调整控件位置,利用复选框改变文字的字形
Private Sub Check1_Click()
If Check1.Value Then
Label1.FontBold = True
Else
Label1.FontBold = False
End if
End Sub
Private Sub Check2_Click()
If Check2.Value Then
Label1.FontItalic = True
Else
Label1.FontItalic = False
End if
End Sub
Private Sub Check3_Click()
If Check3.Value Then
Label1,FontUnderline = True
Else
Label1,FontUnderline = False
End if
End Sub
Private Sub Check4_Click()
If Check4.Value Then
Label1.Font Strikethru = True
Else
Label1.Font Strikethru = False
End if
End Sub
【例4-20】 编写程序,通过单选按钮控制输出内容。
Private Sub Command1_Click()
Text1.Text = ""
If Option1.Value = True Then
Text1.Text = Option1.Caption
End If
If Option2.Value = True Then
Text1.Text = Option2.Caption
End If
If Option3.Value = True Then
Text1.Text = Option3.Caption
End If
If Option4.Value = True Then
Text1.Text = Option4.Caption
End If
If Option5.Value = True Then
Text1.Text = Text1.Text + Option5.Caption + "专业"
End If
If Option6.Value = True Then
Text1.Text = Text1.Text + Option6.Caption + "专业"
End If
If Option7.Value = True Then
Text1.Text = Text1.Text + Option7.Caption + "专业"
End If
If Option8.Value = True Then
Text1.Text = Text1.Text + Option8.Caption + "专业"
End If
End Sub
Private Sub Command2_Click()
End
End Sub
【例4-21】 从提供的选择中,选择相应的字体类型、字体大小、是否黑体、是否加下划线,确定后使文本框中的字符串按选定的字体属性显示。
Private Sub Form_Load()
Combo1.AddItem "宋体"
Combo1.AddItem "华文彩云"
Combo1.AddItem "华文新魏"
Combo1.AddItem "华文行楷"
Combo2.AddItem "9"
Combo2.AddItem "10"
Combo2.AddItem "11"
Combo2.AddItem "12"
Combo3.AddItem "是"
Combo3.AddItem "否"
Combo4.AddItem "是"
Combo4.AddItem "否"
End Sub
Private Sub Command1_Click()
If Combo1.Text <> "" Then
Text1.FontName = Combo1.Text
End If
If Combo2.Text <> "" Then
Text1.FontSize = Val(Combo2.Text)
End If
If Combo3.Text = "是" Then
Text1.FontBold = True
Else
Text1.FontBold = False
End If
If Combo4.Text = "是" Then
Text1.FontUnderline = True
Else
Text1.FontUnderline = False
End If
End Sub
【例4-22】 例如,可以通过滚动条来改变一个物体的运行速度。
Private Sub HScroll1_Change()
Label2.Caption = Str$(HScroll1.Value)
End Sub
Private Sub HScroll1_Scroll()
Label2.Caption = Str$(HScroll1.Value)
End Sub
【例4-23】 用计时器控件建立一个数字计时器,在窗体上显示当前系统时间。
Private Sub Timer1_Timer()
Label2.Caption = Time
End Sub