Text1.FontName = \黑体\ End If
If Option3 Then
Text1.FontSize = 8 Else
Text1.FontSize = 10 End If End Sub
Private Sub Command2_Click() End End Sub 【例4-5】
Private Sub Form_Load()
‘在窗体的Load事件中输入列表框的各个项目
lstBooks.AddItem \计算机应用基础\ lstBooks.AddItem \操作系统\ lstBooks.AddItem \数据结构\
lstBooks.AddItem \网络技术基础\End Sub
Private Sub cmdAdd_Click()
‘单击添加命令按钮时将文本框中输入的内容添加到列表框中 lstBooks.AddItem txtItem txtItem = \End Sub
Private Sub cmdDelete_Click() ‘删除列表框中选中的项目
lstBooks.RemoveItem lstBooks.ListIndex End Sub
Private Sub cmdModify_Click()
‘所选项目显示在文本框中,等待修改 txtItem.Text = lstBooks.Text cmdAdd.Enabled = False cmdDelete.Enabled = False cmdModify.Enabled = False cmdModify_OK.Enabled = True End Sub
Private Sub cmdModify_OK_Click()
‘所选项目显示在文本框中修改完成后,按下修改确定按钮,更新列表框内容 lstBooks.List(lstBooks.ListIndex) = txtItem cmdModify_OK.Enabled = True cmdAdd.Enabled = True
6
cmdDelete.Enabled = True cmdModify.Enabled = True txtItem = \End Sub 【例4-6】
Private Sub Form_Click() Picture3.Picture=Picture1.Picture Picture1.Picture=Picture2.Picture Picture2.Picture=Picture3.Picture Pciture3.Picture=LoadPicture() ?把第3个图片框设置为空 End sub 【例4-7】
Private Sub HScroll1_Change()
Text1.Text = HScroll1.Value End Sub 【例4-8】
Private Timer1_Timer() Labe1.Caption=Time$ End Sub 实训
Private Sub Command1_Click() Debug.Print \姓名:\
Debug.Print \出生年月:\Debug.Print \籍贯:\
If Option1.Value Then Debug.Print \性别:\男\If Option2.Value Then Debug.Print \性别:\女\Debug.Print \民族:\
If Check1.Value = 1 Then Debug.Print \健康\不健康\Debug.Print \职称:\Debug.Print \工资:\
Debug.Print \外语熟练程度\Debug.Print \简历:\
End Sub 第5章 课前体验
Private Sub Command6_Click()
Const n = 10 ‘定义常量n的值为10
Max = 0: K = 0 ‘最高分及所在位置赋初值 For i = 1 To n
b(i) = InputBox(\请输入第\个同学的成绩\求最高分) ‘输入成绩 If b(i) > Max Then Max = b(i)
K = I ‘将第i个成绩与最高分Max相比,如果比最高分高,则保存起来 End If
7
Next i
Print\最高分是第\个同学,其成绩是:\End Sub 【例5-1】
Option Base 1
Private Sub Command1_Click()
Dim a(3) As Integer
a(1) = 1: a(2) = 3: a(3) = 5 Print a(1) Print a(2) Print a(3)
End Sub 【例5-2】
For i=1 To 10
b(i) = InputBox(\请输入第\个数\
Next i
【例5-3】
For i=1 To 2 For j=1 To 2 b(i,j) = i+j Next j
Next i
【例5-4】
Dim S(3,2) As Integer 程序如下: For i = 0 To 3
Print Tab(5); ?输出位置定位 For j = 0 To 2
S(i, j) = i * 2 + j ‘给各元素赋值 Print S(i, j); 图5-4 使用二维数组打印出的图形 Next j
Print ‘换行 Next i 【例5-5】
Private Sub Form_Click()
Dim s(5) As Integer '定义数组S Const n = 5 For i = 1 To n
s(i) = Val(InputBox(\请输入第\个数\数据排序))
‘输入n个数,转换成数值后保存在数组中。
Next i
For i = 1 To n-1 ‘进行n-1趟比较 Max = I ‘对第i遍比较时,初始假定第i个元素最小。 For j = i + 1 To n ‘在数组i~n个元素中选最小元素
8
If s(j) < s(Max) Then Max = j Next j t = s(i)
s(i) = s(Max) s(Max) = t ‘i~n个元素中选出的最小元素与第i个元素交换
Next i
For i = 1 To 5 Print s(i)
Next i
End Sub 【例5-6】
Dim b() As Integer
Private Sub Form_Click() ReDim b(2) For i = 0 To 2 b(i) = i Next i
ReDim Preserve b(3) b(3) = 7 For i = 0 To 3 Print b(i); Next i End Sub 【例5-7】
Dim a(8, 8) As Integer '定义一个二维数组 Private Sub Form_Click()
‘下面的二层循环语句给数组赋值 For i = 1 To 8 For j = 1 To i
If i = 1 Or j = 1 Then
a(i, j) = 1 '数组中每一行第一个,最后一个数均为1 Else
a(i, j) = a(i - 1, j - 1) + a(i - 1, j)
'数组中其余数据等于它上一行的相邻两列之和
End If Next j Next i
'下面的二层循环语句将数组中的值打印出来 For i = 1 To 8
Print Tab(20 - 2 * i); '定位打印位置 For j = 1 To i
If a(i, j) < 10 Then '将数组中的值转换成长度为3的字符串,可使打印数据整齐 s = \
9
ElseIf a(i, j) < 100 Then s = \ End If Print s; Next j
Print '换行 Next i End Sub 【例5-8】
Private Sub Option1_Click(Index As Integer) Select Case Index Case 0
Label1.ForeColor = RGB(255, 0, 0) Case 1
Label1.ForeColor = RGB(0, 255, 0) Case 2
Label1.ForeColor = RGB(255, 255, 0) End Select End Sub 实训
Option Base 1 ‘定义数组下标从1开始 Dim arr() ‘定义动态数组arr
Private Sub Form_Click()
n = Val(InputBox(\请输入矩阵的行数\‘输入矩阵行数 m = Val(InputBox(\请输入矩阵的列数\‘输入矩阵列数
ReDim arr(n, m) ‘重定义数组arr为n行m列的二
维数组
‘下列程序段为数组输入数据
For i = 1 To n
For j = 1 To m
arr(i, j) = Val(InputBox(\请输入矩阵第\行第\列的数据\
Next j
Next i
‘下列程序段在数组中找出最大值及所在位置
row = 1: col = 1 For i = 1 To n
For j = 1 To m
If arr(i, j) > arr(row, col) Then row = i: col = j End If
Next j Next i
‘下列程序段输出矩阵及最大值及所在位置
10