1【例3-1】PrivateSubForm_Click()c1$=Chr$(13)+Chr$(10)msg1$=请输入您的名字:msg2$=输入后按回车键msg3$=或单击“确定”按钮msg$=msg1$+c1$+msg2$+c1$+msg3$name$=InputBox(msg$,InputBox函数示例,张三)Printname$EndSub【例3-2】PrivateSubForm_Click()Msg1$=”AreyouContinueto?”msg2$=”OperationDialogBox”r=MsgBox(msg1$,34,msg2$)PrintrEndSub【例3-3】编写程序,用MsgBox函数判断是否继续执行。PrivateSubForm_Click()msg$=请确认此数据是否正确Title$=数据检查对话框x=MsgBox(msg$,19,Title$)Ifx=6ThenPrintx*xElseIfx=7ThenPrint请重新输入EndIfEndSub【例3-5】PrivateSubForm_Click()Print:PrintFontName=隶书FontSize=16Print姓名;Tab(8);年龄;Tab(16);职务;PrintTab(24);单位;Tab(32);籍贯PrintPrint吴大明;Tab(8);25;Tab(16);职员;Tab(24);人事科;Tab(32);北京EndSub【例3-6】PrivateSubForm_Click()X=InputBox(请输入成绩,学生成绩录入,00)PrintxEndSub【例3-7】2PrivateSubForm_Click()DimxAsSingle,yAsSinglex=InputBox(“请输入x的值”)Ifx0Theny=1ElseIfx=0Theny=0Elsey=-1Print“x=”;x,”y=”;yEndSub【例3-8】PrivateSubForm_Click()Dimmsg,UserInputmsg=请输入一个字母或0~9之间的数字.UserInput=InputBox(msg)‘输入一个字母或数字IfNotIsNumeric(UserInput)Then‘判断是否是数字IfLen(UserInput)=1Then‘不是数字时,判断输入的字符串长度是否为1SelectCaseAsc(UserInput)‘判断输入字母的ASCII码值Case60To90'在60-90之间为大写字母msg=你输入的是一个大写字母'msg=msg&Chr(Asc(UserInput))&'。Case97To122'小写字母msg=你输入的是一个小写字母'msg=msg&Chr(Asc(UserInput))&'。CaseElsemsg=你没有输入字母或数字.EndSelectEndIfElseSelectCaseVal(UserInput)'将输入的数值型字符转换为数值Case1,3,5,7,9'如果是奇数msg=UserInput&是一个奇数。Case0,2,4,6,8'如果是偶数msg=UserInput&是一个偶数。CaseElse'出界msg=你输入的数字不在0~9范围内EndSelectEndIfMsgBoxmsgEndSub【例3-8】PrivateSubForm_Click()Dimmsg,UserInputmsg=请输入一个字母或0~9之间的数字.UserInput=InputBox(msg)‘输入一个字母或数字IfNotIsNumeric(UserInput)Then‘判断是否是数字IfLen(UserInput)=1Then‘不是数字时,判断输入的字符串长度是否为31SelectCaseAsc(UserInput)‘判断输入字母的ASCII码值Case60To90'在60-90之间为大写字母msg=你输入的是一个大写字母'msg=msg&Chr(Asc(UserInput))&'。Case97To122'小写字母msg=你输入的是一个小写字母'msg=msg&Chr(Asc(UserInput))&'。CaseElsemsg=你没有输入字母或数字.EndSelectEndIfElseSelectCaseVal(UserInput)'将输入的数值型字符转换为数值Case1,3,5,7,9'如果是奇数msg=UserInput&是一个奇数。Case0,2,4,6,8'如果是偶数msg=UserInput&是一个偶数。CaseElse'出界msg=你输入的数字不在0~9范围内EndSelectEndIfMsgBoxmsgEndSub【例3-10】SubForm_Click()DimNAsIntegern=InputBox(EnterN:)‘输入N的值k=1Fori=1ToN‘循环N次,计算出N!k=k*INextiPrintN;”!=”;k‘数据输出EndSub【例3-12】DimS,NS=0:N=0DoWhileS=100N=N+1S=S+NLoopPrintS,N【例3-13】PrivateSubForm_Click()4DimcharAsStringCount=0char=InputBox(请输入一个字符)Whilechar?Count=Count+1char=InputBox$(请输入一个字符)WendPrint输入的字符数是:;CountEndSub【例3-14】PrivateSubForm_Click()Print*;Fori=1To9PrintTab(i*6);i;NextiPrintForj=1To9Printj;Fork=1TojPrintTab(k*6);j*k;;NextkPrintNextjEndSub第4章课前体验(1)假定用来输入数学成绩的文本框名称为Text1,该文本框的LostFocus事件过程如下:PrivateSubText1_LostFocus()IfVal(Text1.Text)0OrVal(Text1.Text)100ThenText1.Text=Text1.SetFocusEndIfEndSub(2)其他文本框的LostFocus事件类似。(3)假定按钮名称为Command1,该按钮的Click事件过程如下:PrivateSubCommand1_Click()IfCheck1.Value=1ThenSum=Sum+Val(Text1.Text)IfCheck2.Value=1ThenSum=Sum+Val(Text2.Text)IfCheck3.Value=1ThenSum=Sum+Val(Text3.Text)IfCheck4.Value=1ThenSum=Sum+Val(Text4.Text)IfCheck5.Value=1ThenSum=Sum+Val(Text5.Text)Text6.Text=SumEndSub5【例4-1】PrivateSubCommand1_Click()‘在其单击事件中编程Fori=1To6‘外循环,控制输出几行Forj=1Toi‘内循环,控制输出几列Print*;NextjPrint‘换行NextiEndSub【例4-2】PrivateSubText1_Change()Text2.Text=LCase(Text1.Text)Text3.Text=UCase(Text1.Text)EndSub【例4-3】PrivateSubCheck1_Click()Text1.FontUnderline=NotText1.FontUnderlineEndSubPrivateSubCheck2_Click()Text1.FontItalic=NotText1.FontItalicEndSubPrivateSubOption1_Click()Text1.Font=黑体EndSubPrivateSubOption2_Click()Text1.Font=宋体EndSub【例4-4】PrivateSubCommand1_Click()IfOption1ThenText1.FontName=宋体ElseText1.FontName=黑体EndIfIfOption3ThenText1.FontSize=8ElseText1.FontSize=10EndIfEndSubPrivateSubCommand2_Click()6EndEndSub【例4-5】PrivateSubForm_Load()‘在窗体的Load事件中输入列表框的各个项目lstBooks.AddItem计算机应用基础lstBooks.AddItem操作系统lstBooks.AddItem数据结构lstBooks.AddItem网络技术基础EndSubPrivateSubcmdAdd_Click()‘单击添加命令按钮时将文本框中输入的内容添加到列表框中lstBooks.AddItemtxtItemtxtItem=EndSubPrivateSubcmdDelete_Click()‘删除列表框中选中的项目lstBooks.RemoveItemlstBooks.ListIndexEndSubPrivateSubcmdModify_Click()‘所选项目显示在文本框中,等待修改txtItem.Text=lstBooks.TextcmdAdd.Enabled=FalsecmdDelete.Enabled=FalsecmdModify.Enabled=FalsecmdModify_OK.Enabled=TrueEndSubPrivateSubcmdModify_OK_Click()‘所选项目显示在文本框中修改完成后,按下修改确定按钮,更新列表框内容lstBooks.List(lstBooks.ListIndex)=txtItemcmdModify_OK.Enabled=TruecmdAdd.Enabled=TruecmdDelete.Enabled=TruecmdModify.Enabled=TruetxtItem=EndSub【例4-6】PrivateSubForm_Click()Picture3.Picture=Picture1.PicturePicture1.Picture=Picture2.PicturePicture2.Picture=Picture3.Picture7Pciture3.Picture=LoadPicture()‘把第3个图片框设置为空Endsub【例4-7】PrivateSubHScroll1_Change()Text1.Text=HScroll1.ValueEndSub【例4-8】PrivateTimer1_Timer()Labe1.Caption=Time$EndSub实训PrivateSubCommand1_Click()Debug.Print姓名:;Text1.TextDebug.Print出生年月:;Text2.TextDebug.Print籍贯:;Text3.TextIfOption1.ValueThenDebug.Print性别:;男IfOption2.ValueThenDebug.Print性别:;女Debug.Print民族:;Combo1.TextIfCheck1.Value=1ThenDebug.Print健康ElseDebug.Print不健康Debug.Print职称:;Combo3.TextDebug.Print工资:;Text4.TextDebug.Print外语熟练程度;HScroll1.ValueDebug.Print简历:;Text5.TextEndSub第5章课前体验PrivateSubCommand6_Click()Constn=10‘定义常量n的值为10Max=0:K=0‘最高分及所在位置赋初值Fori=1Tonb(i)=InputBox(请输入第&i&个同学的成绩,求最高分)‘输入成绩Ifb(i)MaxThenMax=b(i)K