课前体验
Private Sub Form_Click() For i = 1 To 10
For j = 1 To i Print \Next j Print Next i End Sub 【例3-1】
Private Sub Form_Click() c1$ = Chr$(13) + Chr$(10)
msg1$ = \请输入您的名字: \msg2$ = \输入后按回车键\
msg3$ = \或单击“确定”按钮\
msg$ = msg1$ + c1$ + msg2$ + c1$ + msg3$
name$ = InputBox(msg$, \函数示例\张三\Print name$
End Sub 【例3-2】
Private Sub Form_Click() Msg1$=”Are you Continue to?” msg2$=”Operation Dialog Box” r=MsgBox(msg1$,34,msg2$) Print r End Sub
【例3-3】编写程序,用MsgBox函数判断是否继续执行。
Private Sub Form_Click()
msg$ = \请确认此数据是否正确\ Title$ = \数据检查对话框\ x = MsgBox(msg$, 19, Title$) If x = 6 Then Print x * x
ElseIf x = 7 Then
Print \请重新输入\ End If
End Sub 【例3-5】
Private Sub Form_Click() Print: Print
FontName = \隶书\ FontSize = 16
Print \姓名\年龄\职务\ Print Tab(24); \单位\籍贯\
1
Print \吴大明\职员\人事科\北京\End Sub
【例3-6】
Private Sub Form_Click()
X = InputBox(\请输入成绩\学生成绩录入\Print x End Sub 【例3-7】
Private Sub Form_Click() Dim x As Single, y As Single x=InputBox(“请输入x的值”)
If x>0 Then y=1 ElseIf x=0 Then y=0 Else y=-1 Print “x=”; x ,”y=” ; y End Sub 【例3-8】
Private Sub Form_Click() Dim msg, UserInput
msg = \请输入一个字母或0~9之间的数字.\
UserInput = InputBox(msg) ‘输入一个字母或数字 If Not IsNumeric(UserInput) Then ‘判断是否是数字
If Len(UserInput) = 1 Then ‘不是数字时,判断输入的字符串长度是否为1
Select Case Asc(UserInput) ‘判断输入字母的ASCII码值 Case 60 To 90 '在60-90之间为大写字母 msg = \你输入的是一个大写字母'\
msg = msg & Chr(Asc(UserInput)) & \。\ Case 97 To 122 '小写字母 msg = \你输入的是一个小写字母'\
msg = msg & Chr(Asc(UserInput)) & \。\ Case Else
msg = \你没有输入字母或数字.\ End Select End If
Else
Select Case Val(UserInput) '将输入的数值型字符转换为数值 Case 1, 3, 5, 7, 9 '如果是奇数 msg = UserInput & \是一个奇数。\ Case 0, 2, 4, 6, 8 '如果是偶数 msg = UserInput & \是一个偶数。\ Case Else '出界 msg = \你输入的数字不在0~9范围内\ End Select End If
2
MsgBox msg End Sub 【例3-8】
Private Sub Form_Click() Dim msg, UserInput
msg = \请输入一个字母或0~9之间的数字.\
UserInput = InputBox(msg) ‘输入一个字母或数字 If Not IsNumeric(UserInput) Then ‘判断是否是数字
If Len(UserInput) = 1 Then ‘不是数字时,判断输入的字符串长度是否为1
Select Case Asc(UserInput) ‘判断输入字母的ASCII码值 Case 60 To 90 '在60-90之间为大写字母 msg = \你输入的是一个大写字母'\
msg = msg & Chr(Asc(UserInput)) & \。\ Case 97 To 122 '小写字母 msg = \你输入的是一个小写字母'\
msg = msg & Chr(Asc(UserInput)) & \。\ Case Else
msg = \你没有输入字母或数字.\ End Select End If
Else
Select Case Val(UserInput) '将输入的数值型字符转换为数值 Case 1, 3, 5, 7, 9 '如果是奇数 msg = UserInput & \是一个奇数。\ Case 0, 2, 4, 6, 8 '如果是偶数 msg = UserInput & \是一个偶数。\ Case Else '出界 msg = \你输入的数字不在0~9范围内\ End Select End If
MsgBox msg End Sub 【例3-10】
Sub Form_Click()
Dim N As Integer
n = InputBox(\ N:\ ‘输入N的值 k = 1
For i = 1 To N ‘循环N次,计算出N! k = k * I Next i
Print N;”!=”;k ‘数据输出 End Sub
【例3-12】
3
Dim S, N S = 0: N = 0
Do While S <= 100 N = N + 1 S = S + N Loop Print S, N 【例3-13】
Private Sub Form_Click() Dim char As String Count = 0
char = InputBox(\请输入一个字符\While char <> \ Count = Count + 1
char = InputBox$(\请输入一个字符\Wend
Print \输入的字符数是:\End Sub
【例3-14】
Private Sub Form_Click() Print \For i = 1 To 9
Print Tab(i * 6); i; Next i Print
For j = 1 To 9 Print j;
For k = 1 To j
Print Tab(k * 6); j * k; \ Next k Print Next j End Sub
第4章 课前体验
(1)假定用来输入数学成绩的文本框名称为Text1,该文本框的LostFocus事件过程如下:
Private Sub Text1_LostFocus()
If Val(Text1.Text) < 0 Or Val(Text1.Text) > 100 Then Text1.Text = \ Text1.SetFocus
End If End Sub
(2)其他文本框的LostFocus事件类似。
4
(3)假定按钮名称为Command1,该按钮的Click事件过程如下:
Private Sub Command1_Click()
If Check1.Value = 1 Then Sum = Sum + Val(Text1.Text) If Check2.Value = 1 Then Sum = Sum + Val(Text2.Text) If Check3.Value = 1 Then Sum = Sum + Val(Text3.Text) If Check4.Value = 1 Then Sum = Sum + Val(Text4.Text) If Check5.Value = 1 Then Sum = Sum + Val(Text5.Text) Text6.Text = Sum End Sub
【例4-1】
Private Sub Command1_Click() ‘在其单击事件中编程
For i = 1 To 6 ‘外循环,控制输出几行
For j = 1 To i ‘内循环,控制输出几列
Print \Next j
Print ‘换行 Next i
End Sub 【例4-2】
Private Sub Text1_Change() Text2.Text = LCase(Text1.Text) Text3.Text = UCase(Text1.Text) End Sub 【例4-3】
Private Sub Check1_Click()
Text1.FontUnderline = Not Text1.FontUnderline End Sub
Private Sub Check2_Click()
Text1.FontItalic = Not Text1.FontItalic End Sub
Private Sub Option1_Click() Text1.Font = \黑体\End Sub
Private Sub Option2_Click() Text1.Font = \宋体\End Sub 【例4-4】
Private Sub Command1_Click() If Option1 Then
Text1.FontName = \宋体\ Else
5