收藏 分享(赏)

VB实例源码.doc

上传人:tangtianxu1 文档编号:3083011 上传时间:2018-10-03 格式:DOC 页数:66 大小:412KB
下载 相关 举报
VB实例源码.doc_第1页
第1页 / 共66页
VB实例源码.doc_第2页
第2页 / 共66页
VB实例源码.doc_第3页
第3页 / 共66页
VB实例源码.doc_第4页
第4页 / 共66页
VB实例源码.doc_第5页
第5页 / 共66页
点击查看更多>>
资源描述

1、1课前体验Private Sub Form_Click()For i = 1 To 10For j = 1 To iPrint “* “;Next jPrintNext iEnd Sub【例 3-1】Private Sub Form_Click()c1$ = Chr$(13) + Chr$(10)msg1$ = “请输入您的名字: “msg2$ = “输入后按回车键“msg3$ = “或单击“确定”按钮“msg$ = msg1$ + c1$ + msg2$ + c1$ + msg3$name$ = InputBox(msg$, “InputBox 函数示例“, “张三“)Print name$

2、End Sub【例 3-2】Private Sub Form_Click()Msg1$=”Are you Continue to?”msg2$=”Operation Dialog Box”r=MsgBox(msg1$,34,msg2$)Print rEnd Sub【例 3-3】编写程序,用 MsgBox 函数判断是否继续执行。Private Sub Form_Click()msg$ = “请确认此数据是否正确“Title$ = “数据检查对话框“x = MsgBox(msg$, 19, Title$)If x = 6 ThenPrint x * xElseIf x = 7 ThenPrint

3、“请重新输入“End IfEnd Sub【例 3-5】Private Sub Form_Click()Print: PrintFontName = “隶书 “FontSize = 16Print “ 姓名“; Tab(8); “ 年龄“; Tab(16); “ 职务 “;Print Tab(24); “单位“; Tab(32); “籍贯“2PrintPrint “吴大明“; Tab(8); 25; Tab(16); “职员“; Tab(24); “人事科“; Tab(32); “北京“End Sub【例 3-6】Private Sub Form_Click()X = InputBox(“请输入

4、成绩“, “学生成绩录入“, “00“)Print xEnd Sub【例 3-7】Private Sub Form_Click()Dim x As Single, y As Singlex=InputBox(“请输入 x 的值”)If x0 Then y=1 ElseIf x=0 Then y=0 Else y=-1Print “x=”; x ,”y=” ; yEnd Sub【例 3-8】Private Sub Form_Click()Dim msg, UserInputmsg = “请输入一个字母或 09 之间的数字.“UserInput = InputBox(msg) 输入一个字母或数字I

5、f Not IsNumeric(UserInput) Then 判断是否是数字If Len(UserInput) = 1 Then 不是数字时,判断输入的字符串长度是否为 1Select Case Asc(UserInput) 判断输入字母的 ASCII 码值Case 60 To 90 在 60-90 之间为大写字母msg = “你输入的是一个大写字母 “msg = msg ”!=”;k 数据输出End Sub【例 3-12】Dim S, NS = 0: N = 0Do While S “?“Count = Count + 1char = InputBox$(“请输入一个字符 “)WendPr

6、int “输入的字符数是:“; CountEnd Sub【例 3-14】Private Sub Form_Click()Print “ *“;For i = 1 To 9Print Tab(i * 6); i;Next iPrintFor j = 1 To 9Print j;For k = 1 To jPrint Tab(k * 6); j * k; “ “;Next kPrintNext jEnd Sub5第 4 章课前体验(1)假定用来输入数学成绩的文本框名称为 Text1,该文本框的 LostFocus 事件过程如下:Private Sub Text1_LostFocus()If Val

7、(Text1.Text) 100 ThenText1.Text = “Text1.SetFocusEnd IfEnd Sub(2)其他文本框的 LostFocus 事件类似。(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(Te

8、xt3.Text)If Check4.Value = 1 Then Sum = Sum + Val(Text4.Text)If Check5.Value = 1 Then Sum = Sum + Val(Text5.Text)Text6.Text = SumEnd Sub【例 4-1】Private Sub Command1_Click() 在其单击事件中编程For i = 1 To 6 外循环,控制输出几行For j = 1 To i 内循环,控制输出几列Print “ * “;Next jPrint 换行Next iEnd Sub【例 4-2】Private Sub Text1_Chang

9、e()Text2.Text = LCase(Text1.Text)Text3.Text = UCase(Text1.Text)End Sub【例 4-3】Private Sub Check1_Click()Text1.FontUnderline = Not Text1.FontUnderlineEnd SubPrivate Sub Check2_Click()6Text1.FontItalic = Not Text1.FontItalicEnd SubPrivate Sub Option1_Click()Text1.Font = “黑体“End SubPrivate Sub Option2_C

10、lick()Text1.Font = “宋体“End Sub【例 4-4】Private Sub Command1_Click()If Option1 ThenText1.FontName = “宋体“ElseText1.FontName = “黑体“End IfIf Option3 ThenText1.FontSize = 8ElseText1.FontSize = 10End IfEnd SubPrivate Sub Command2_Click()EndEnd Sub【例 4-5】Private Sub Form_Load()在窗体的 Load 事件中输入列表框的各个项目lstBooks

11、.AddItem “计算机应用基础 “lstBooks.AddItem “操作系统 “lstBooks.AddItem “数据结构 “lstBooks.AddItem “网络技术基础 “End SubPrivate Sub cmdAdd_Click()单击添加命令按钮时将文本框中输入的内容添加到列表框中lstBooks.AddItem txtItemtxtItem = “End SubPrivate Sub cmdDelete_Click()删除列表框中选中的项目lstBooks.RemoveItem lstBooks.ListIndexEnd Sub7Private Sub cmdModif

12、y_Click()所选项目显示在文本框中,等待修改txtItem.Text = lstBooks.TextcmdAdd.Enabled = FalsecmdDelete.Enabled = FalsecmdModify.Enabled = FalsecmdModify_OK.Enabled = TrueEnd SubPrivate Sub cmdModify_OK_Click()所选项目显示在文本框中修改完成后,按下修改确定按钮,更新列表框内容lstBooks.List(lstBooks.ListIndex) = txtItemcmdModify_OK.Enabled = TruecmdAdd

13、.Enabled = TruecmdDelete.Enabled = TruecmdModify.Enabled = TruetxtItem = “End Sub【例 4-6】Private Sub Form_Click()Picture3.Picture=Picture1.PicturePicture1.Picture=Picture2.PicturePicture2.Picture=Picture3.PicturePciture3.Picture=LoadPicture() 把第 3 个图片框设置为空End sub【例 4-7】Private Sub HScroll1_Change()Te

14、xt1.Text = HScroll1.ValueEnd Sub【例 4-8】Private Timer1_Timer()Labe1.Caption=Time$End Sub实训Private Sub Command1_Click()Debug.Print “姓名:“; Text1.TextDebug.Print “出生年月:“; Text2.TextDebug.Print “籍贯:“; Text3.TextIf Option1.Value Then Debug.Print “性别:“; “ 男“If Option2.Value Then Debug.Print “性别:“; “ 女“Debu

15、g.Print “民族:“; Combo1.TextIf Check1.Value = 1 Then Debug.Print “健康“ Else Debug.Print “不健康“Debug.Print “职称:“; Combo3.Text8Debug.Print “工资:“; Text4.TextDebug.Print “外语熟练程度“; HScroll1.ValueDebug.Print “简历:“; Text5.TextEnd Sub第 5 章课前体验Private Sub Command6_Click()Const n = 10 定义常量 n 的值为 10Max = 0: K = 0

16、最高分及所在位置赋初值For i = 1 To n b(i) = InputBox(“请输入第“ 输出位置定位For j = 0 To 29S(i, j) = i * 2 + j 给各元素赋值Print S(i, j);Next jPrint 换行Next i【例 5-5】Private Sub Form_Click()Dim s(5) As Integer 定义数组 SConst n = 5For i = 1 To ns(i) = Val(InputBox(“请输入第“ Next jPrint 换行Next iPrintPrint “矩阵中最大值为:“;arr(row, col); “其位置

17、在第“ t1For I = 2 To 10b(i) = i * 2Next is1 = ss(b()Print“第二个数组各元素之和 s1=“; s1End Sub【例 6-8】(1)先定义一个具有可选参数的函数过程 sum,用来进行 3 个数的加法运算。Private Function sum(x As Integer,Optional y As Integer,_Optional z As Integer=3)As IntegerSum = x + y + zEnd Function(2)编写窗体的单击事件过程,如下:Private Sub Form_Click()Print“sum(1)

18、 = 1 + 0 + 3 = “;sum(1) 省略两个参数Print“sum(1,2) = 1 + 2 + 3 = “;sum(1,2) 省略第 3 个参数Print“sum(1,8) = 1 + 0 + 8 = “;sum(1,8) 省略第 2 个参数Print“sum(1,4,8) = 1 + 4 + 8 =“;sum(1,4,8) 不省略参数End Sub【例 6-9】15(1)先定义一个具有可变参数的函数过程 MySum,如下:Function MySum(ParamArray VA() As Integer 声明为可变参数Dim i As IntegerDim Sum As Int

19、egerSum = 0For i=LBound(VA) To UBound(VA) 得到数组的大小,并进行循环Sum = Sum+VA(i)NextMySum = SumEnd Function(2)编写窗体的单击事件过程,如下:Private Sub Form_Click()Dim s As IntegerPrintPrint Tab(2); “使用 3 个实参:“;S = MySum(2,4,6) 可以使用任意多个实参来调用Print “MySum(2,4,6)=“; sPrintPrint Tab(2); “使用 5 个实参:“;S = MySum(1,2,3,4,5) Print “M

20、ySum(1,2,3,4,5)=“;sEnd Sub【例 6-10】(1)定义具有窗体参数的过程,如下:Private Sub BiaoTi(fm As Form) fm 为窗体对象参数Text1.Text = “窗体的标题是“a=“;a;“b=“;bCall s1 调用通用过程 sub1PrintPrint“ 调用 s1 后,事件过程中的变量:“;“a=“;a;“b=“;bEnd SubSub s1() 通用过程Dim a As Integer,b As Integer 过程级变量A = 55:b = 66PrintPrint“通用过程 s1 中的变量:“;“a=“;a;“b=“;bEnd

21、Sub【例 6-13】Dim a As Integer,b As Integer 声明模块级变量Private Sub Command1_Click()A = 100:b = 8 对模块级变量赋值PrintPrint“调用 s1 前,模块级变量:“;“a=“;a;“b=“;bCall s1 调用通用过程 sub1PrintPrint“调用 s1 后,模块级变量:“;“a=“;a;“b=“;bEnd SubSub s1() 通用过程A = 55:b = 66 对模块级变量赋值PrintPrint“通用过程 s1 对模块级变量赋值:“;“a=“;a;“b=“;bEnd Sub【例 6-14】(4)

22、在窗体 Form1 的代码窗口的顶部,声明模块级变量 a 和 b,分别用来储存程序运行后单击左右两个命令按钮的次数。代码如下:17Private a As IntegerPrivate b As Integer(5)编写左边的命令按钮的 Command1_Clic 事件过程。代码如下:Private Sub Command1_Click()Dim s As StringI = I + 1A = a + 1S = “单击按钮“d,“静态变量 s = “;sEnd SubPrivate Sub Command1_Click()Dim i As IntegerFor i = 1 To 3change

23、 或 Call changeNext iEnd Sub实训(3)在 Form1 的窗体模块的代码窗口的最顶部(通用声明段)声明模块级变量 title,用来存储字符串。代码如下:Private title As String 表明是使用通用过程还是函数(4)定义 Sub 通用过程 MySub,来进行乘法运算。其中的形参 x、y 按值传递,形参 z按地址传递。代码如下:Private Sub MySub(ByVal x As Integer,ByVal y As Integer,z As Integer)Z = x * yEnd Sub定义函数过程 MyFun,来进行加法运算。其中的形参 m 和

24、n 是按值传递的。代码如下:18Private Function MyFun(ByVal m As Integer,ByVal n As Integer) As IntegerMyFun=m+nEnd Function(5)添加窗体的事件过程 Form_Load,来做些初始化的工作,将文本框置空。代码如下:Private Sub Form_Load()Text1.Text = “Text2.Text = “Text3.Text = “End Sub分别添加两个单选按钮的 Click 事件过程,来设置框架 Frame1 和标签 Label 的 Caption属性。代码如下:Private Sub

25、 Option1_Click()Frame1.Caption = “乘法积运算“Label1.Caption = “End SubPrivate Sub Option2_Click()Frame1.Caption = “加法运算“Label1.Caption = “+“End Sub添加命令按钮的事件过程 Command1_Click。其中,关键字 static 声明的静态变量 i 和 j用来储存运算的次数,关键字 Dim 声明的过程级局部变量 a、b、c 用来储存三个文本框里的数值。代码如下:Private Sub Command1_Click()Static i As Integer,j

26、As IntegerDim a As Integer,b As Integer,c As IntegerA = Val(Text1.Text)B = Val(Text2.Text)If Option1.Value=True ThenTitle = “用过程运算“Call MySub(a,b,c)i=i+1Form1.Caption = “第“XElseClsPrint“鼠标指针当前纵坐标 Y=“;YEnd IfEnd Sub【例 7-3】1)首先,在窗体模块的顶部声明一个逻辑变量 paint,如下:Private paint As Boolean(2)定义窗体上的按下鼠标按键的事件过程,使得变

27、量 paint 的值在按鼠标左键时为true。再定义释放鼠标按键的事件过程,使得变量 paint 的值为 false。代码如下:Private Sub Form_MouseDown(Button As Integer,Shift As Integer,_X As Single,Y As Single)If Button=1 Thenpaint=TrueEnd If20End SubPrivate Sub Form_MouseUp(Button As Integer, Shift As Integer, _X As Single, Y As Single)paint = FalseEnd Sub

28、(3)定义窗体上的鼠标移动事件过程。Private Sub Form_MouseMove(Button As Integer, Shift As Integer, _X As Single, Y As Single)If paint Then paint 为 true 时,程序画出轨迹点PSet(X,Y)End IfEnd Sub【例 7-4】(2)定义窗体的 Form_load 事件过程,使列表框中添加几个选项。代码如下:Private Sub Form_Load()List1.AddItem“0-Default“List1.AddItem“1-Arrow“List1.AddItem“2-C

29、ross“List1.AddItem“3-I-Beam“End Sub(3)定义列表框的单击事件过程。Private Sub List1_Click()Form1.MousePointer=List1.ListIndexEnd Sub【例 7-5】Private Sub Form_KeyPress(KeyAscii As Integer)If KeyAscii = 97 And KeyAscii = 65 And KeyAscii“ThenmenuCut.Enabled = TruemenuCopy.Enabled = TrueEnd IfPopupMenu EditEnd IfEnd Sub

30、为“编辑”菜单中的各菜单项编写单击事件过程Private Sub menuCut_Click()Clip = RichTextBox1.SelTextRichTextBox1.SelText = “ 将选定的字符清除menuCut.Enabled = FalsemenuCopy.Enabled = False 将“剪切“与“ 复制“菜单项设为无效menuPaste.Enabled = TrueEnd SubPrivate Sub menuCopy_Click()clip = RichTextBox1.SelTextmenuCut.Enabled = FalsemenuCopy.Enabled

31、= FalsemenuPaste.Enabled = TrueEnd SubPrivate Sub menuPaste_Click()RichTextBox1.SelText = clip 将变量 clip 中的内容粘贴到光标所在处End Sub上述 3 个菜单项的事件过程分别实现了 RichTextBox1 上的复制、剪切和粘贴的功能。Private Sub menuSelAll_Click()RichTextBox1.SelStart = 0RichTextBox1.SelLength = Len(RichTextBox1.Text)End Sub第 9 章课前体验(3)双击窗体,在窗体的 Load 事件中编写如下程序:Dim Snow(1000, 2), Amounty As Integer Private Sub Form_Load() Form1.Show DoEvents Randomize Amounty = 325 For J = 1 To Amounty Snow(J,0) = Int(Rnd Form1.Width) Snow(J,1) = Int(Rnd Form1.Height)

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 实用文档 > 往来文书

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报