1 / 28

Visual Basic 程序设计教程

Visual Basic 程序设计教程. 第 8 章 变量与过程的作用范围. 8.4 高级变量 8.4.1 使用多个同名的变量 8.4.2 公有变量与局部变量的比较 8.4.3 阴影窗体属性和控件 8.4.4 使用同名的变量和过程 8.5 用户定义类型 8.5.1 用户定义类型的概念 8.5.2 创建用户定义类型 8.5.3 建立和使用用户定义类型变量 8.5.4 用户定义类型数组 8.5.5 程序举例 习题八. 8.1 代码模块的概念 8.1.1 窗体模块 8.1.2 标准模块 8.1.3 类模块

bozica
Download Presentation

Visual Basic 程序设计教程

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Visual Basic 程序设计教程

  2. 第8章 变量与过程的作用范围 8.4高级变量 8.4.1 使用多个同名的变量 8.4.2 公有变量与局部变量的比较 8.4.3 阴影窗体属性和控件 8.4.4 使用同名的变量和过程 8.5 用户定义类型 8.5.1 用户定义类型的概念 8.5.2 创建用户定义类型 8.5.3 建立和使用用户定义类型变量 8.5.4 用户定义类型数组 8.5.5 程序举例 习题八 8.1代码模块的概念 8.1.1 窗体模块 8.1.2 标准模块 8.1.3 类模块 8.2 变量的作用范围 8.2.1 过程级变量 8.2.2 模块级变量 8.2.3 变量的生存期 8.3 过程的作用范围 8.3.1 模块级过程 8.3.2 全局级过程 8.3.3 调用其他模块中的过程

  3. 8.1 代码模块的概念

  4. 8.2.1 过程级变量 【例8-1】过程级局部变量示例。 Private Sub Form_Activate() Dim a As Integer, b As Integer, c As Integer ‘ 过程级局部变量 a = 5: b = 3 Print Print Tab(15); "a"; Tab(25); "b"; Tab(35); "c=a*b" Print "调用prod前"; Tab(14); a; Tab(24); b; Tab(34); c Call prod Print "调用prod后"; Tab(14); a; Tab(24); b; Tab(34); c Print Print "调用sum前"; Tab(14); a; Tab(24); b; Tab(34); c Call sum Print "调用sum后"; Tab(14); a; Tab(24); b; Tab(34); c End Sub

  5. Sub prod() ' 通用过程 Dim a As Integer, b As Integer, c As Integer ' 过程级局部变量 c = a * b Print "prod子程序"; Tab(14); a; Tab(24); b; Tab(34); c End Sub Sub sum() ' 通用过程 Dim a As Integer, b As Integer, c As Integer ' 过程级局部变量 c = a + b Print "sum子程序"; Tab(14); a; Tab(24); b; Tab(34); c End Sub

  6. 【例8-2】公有的模块级全局变量示例。 Public a As Integer, b As Integer, c As Integer ' 写在“(通用)”的“(声明)”中 Private Sub Form_Activate() ' 事件过程 a = 5: b = 3 Print Tab(15); "a"; Tab(25); "b"; Tab(35); "c=a*b" Print "调用prod前"; Tab(14); a; Tab(24); b; Tab(34); c Call prod Print "调用prod后"; Tab(14); a; Tab(24); b; Tab(34); c Print Print Tab(15); "a"; Tab(25); "b"; Tab(35); "c=a+b" Print "调用sum前"; Tab(14); a; Tab(24); b; Tab(34); c Call sum Print "调用sum后"; Tab(14); a; Tab(24); b; Tab(34); c End Sub

  7. Sub prod() ' 通用过程 c = a * b Print "prod子程序"; Tab(14); a; Tab(24); b; Tab(34); c End Sub Sub sum() ' 通用过程 c = a + b Print "prod子程序"; Tab(14); a; Tab(24); b; Tab(34); c End Sub

  8. 【例8-3】下面程序说明了Static关键字的作用。 Private Sub Form_Activate() Dim i As Integer For i = 1 To 6 testsub Next i End Sub Sub testsub() Dim x As Integer, m As String Static y, n x = x + 1: y = y + 1 m = m & "*": n = n & "*" Print "x="; x; " y="; y, "m="; m, "n="; n End Sub

  9. 8.3 过程的作用范围 8.3.1 模块级过程 8.3.2 全局级过程 8.3.3 调用其他模块中的过程 【例8-4】全局级过程的调用,如图8-10所示。 应用程序(工程)中包括两个窗体Forml、Form2和一个标准模块Module1。在Forml窗体中定义了一个计算矩形面积的全局级Function过程,在标准模块Module1中定义了一个计算矩形周长的全局级Function过程。 两个窗体中的命令按钮Click事件过程的功能相同,差别是调用Function过程时所使用的名字。

  10. Form1窗体模块中的过程代码如下: Public Function mian_ji(x As Single, y As Single) As Single mian_ji = x * y End Function Private Sub Command1_Click(Index As Integer) Dim a As Single, b As Single a = Val(Text1(0).Text) b = Val(Text1(1).Text) n = Index If n = 0 Then Label2(0).Caption = mian_ji(a, b) Else Label2(1).Caption = zhou_chang(a, b) End If End Sub Private Sub Form_Load() Form2.Show End Sub

  11. Form2窗体模块中的过程代码如下: Private Sub Command1_Click(Index As Integer) Dim a As Single, b As Single a = Val(Text1(0).Text) b = Val(Text1(1).Text) n = Index If n = 0 Then Label2(0).Caption = Form1.mian_ji(a, b) Else Label2(1).Caption = zhou_chang(a, b) End If End Sub 标准模块Module1中的过程代码: Public Function zhou_chang(x As Single, y As Single) As Single zhou_chang = 2 * (x + y) End Function

  12. 8.4.1 使用多个同名的变量 【例8-5】使用同名的变量,如图8-11所示。 设计方法如下: (1) 在一个新工程中插入两个标准模块,并在窗体上增加一个标签Label1、一个文本框Text1和3个命令按钮Command1~Command3(如图8-11)。 (2) 在第1个标准模块Module1之中声明一个变量x,并编写模块中的Test过程设置它的值: Public x As String ' 声明Module1的x Sub Test() x = "标准模块1" ' 设置Module1的x变量的值 End Sub

  13. (3) 在第2个标准模块Module2中声明了第2个变量x,它有相同的名字。又是名为Test 的过程设置它的值: Public x As String ' 声明Module2的x Sub Test() x = "标准模块2" ' 设置Module2的x变量的值 End Sub (4) 在窗体模块中声明第3个变量x。名为Test的过程又一次设置它的值: Public x As String ' 声明了该窗体的x变量。 Sub Test() x = "窗体模块" ' 设置form中的x变量值 End Sub

  14. (5) 在3个命令按钮的Click事件过程中,每一个都调用了相应的Test过程,并在文本框中显示这3个变量的值。 Private Sub Command1_Click() Module1.Test ' 调用Module1中的Test Text1.Text = Module1.x ' 显示Module1的x End Sub Private Sub Command2_Click() Module2.Test ' 调用Module2中的Test Text1.Text = Module2.x ' 显示Module2的x End Sub Private Sub Command3_Click() Test ' 调用Form中的Test Text1.Text = x ' 显示Form的x End Sub

  15. 8.4.2 公有变量与局部变量的比较 【例8-6】公有变量与局部变量的比较,如图8-12所示。 设计方法如下: (1) 选择新建一个新工程,进入窗体设计器。在窗体上增加一个标签Label1、一个文本框Text1和两个命令按钮Command1~Command2(如图8-12)。 (2) 编写代码。 首先在窗体级声明公有变量x: Public x As String

  16. 编写通用过程Test: Sub Test() Dim x As String ' 声明过程级变量x x = "局部变量" ' 将过程级变量x的值设置为"局部变量" Text1.Text = x ' 显示过程级变量x的值 End Sub 在窗体的Load事件代码中为公有变量赋值: Private Sub Form_Load() x = "公有变量" ' 将Form1.x的值设置为"公有变量" End Sub 编写命令按钮的Click事件代码: Private Sub Command1_Click() Text1.Text = x ' 显示窗体级变量x的值 End Sub Private Sub Command2_Click() Test ' 调用过程,显示过程级变量x的值 End Sub

  17. 8.5.2 创建用户定义类型 可以用Type语句创建用户定义的类型,该语句必须置于模块的声明部分。其格式为 [Private | Public] Type 〈用户类型名〉 〈字段名1〉 As 〈类型名1〉 [〈字段名2〉 As 〈类型名2〉] … [〈字段名n〉 As 〈类型名n〉] End Type

  18. 【例8-7】把数据值分别赋给stu变量中的各个字段。【例8-7】把数据值分别赋给stu变量中的各个字段。 首先在窗体模块的通用段创建用户定义类型: Private Type studentrec stunum As String * 6 ' 学号元素为6个字符的定长字符串 names As String * 8 ' 姓名元素为8个字符的定长字符串 credit As Integer ' 学分元素为整型 avg As Single ' 平均成绩为单精度型 End Type

  19. 编写命令按钮的Click事件代码: Private Sub Command1_Click() Dim stu As studentrec ' 定义一个具有studentrec类型的变量stu stu.stunum = "990001" stu.names = “王 平” stu.credit = 65 stu.avg = 88 Text1(0).Text = stu.stunum Text1(1).Text = stu.names Text1(2).Text = stu.credit Text1(3).Text = stu.avg End Sub

  20. 【例8-8】假设某班有50位学生,每位学生一个记录,定义一个包含50个元素的用户定义数组,并给第32位学生赋值。【例8-8】假设某班有50位学生,每位学生一个记录,定义一个包含50个元素的用户定义数组,并给第32位学生赋值。 首先在窗体模块的通用段创建用户定义类型: Private Type studentrec stunum As String * 6 names As String * 8 credit As Integer avg As Single End Type

  21. 编写命令按钮的Click事件代码: Private Sub Command1_Click() Dim student(1 To 50) As studentrec ' 定义记录数组 student(32).stunum = "960001" student(32).names = "张大力" student(32).credit = 65 student(32).avg = 88 Text1(0).Text = student(32).stunum Text1(1).Text = student(32).names Text1(2).Text = student(32).credit Text1(3).Text = student(32).avg End Sub

  22. 【例8-9】输入学生的姓名、学号、语文分数、英语分数、数学分数,计算每名学生的个人平均成绩,并显示学生的各科成绩,如图8-14所示。【例8-9】输入学生的姓名、学号、语文分数、英语分数、数学分数,计算每名学生的个人平均成绩,并显示学生的各科成绩,如图8-14所示。 设计方法如下: (1) 选择新建一个新工程,进入窗体设计器。 (2) 编写代码。首先在窗体的通用段创建用户定义类型并且声明变量: Private Type studentrec na As String * 6 ' 姓名变量定义为6个字符长度 no As String * 5 ' 学号变量定义为5个字符长度 ch As Single ' 语文变量定义为单精度数 en As Single ' 英语变量定义为单精度数 ma As Single ' 数学变量定义为单精度数 ag As Single ' 平均成绩定义为单精度数 End Type Dim stu() As studentrec ' 定义记录数组

  23. 编写窗体的Load事件代码: Private Sub Form_Load() ReDim stu(0) End Sub 编写“输入”按钮Command1的Click事件代码: Private Sub Command1_Click() n = UBound(stu) ReDim stu(n + 1) With stu(n + 1) .no = Text1(0).Text .na = Text1(1).Text .ch = Text1(2).Text .en = Text1(3).Text .ma = Text1(4).Text

  24. .ag = Int((.ch + .en + .ma) / 3 * 100) / 100 ' 求个人平均成绩 cc = Format(.ch, "@@@@") & Format(.en, "@@@@") & _ Format(.ma, "@@@@") & Format(Str(.ag), "@@@@") List1.AddItem Format(RTrim(.no), "@@@@@@") & Format(RTrim(.na), "@@@") & cc End With Text1(0).SetFocus End Sub 编写“删除”按钮Command2的Click事件代码: Private Sub Command2_Click() If List1.ListIndex = –1 Then MsgBox "请选定欲删除的项!" Exit Sub End If n = List1.ListIndex + 1

  25. For i = n To UBound(stu) – 1 stu(i) = stu(i + 1) Next List1.RemoveItem n - 1 Text1(0).Text = stu(1).no Text1(1).Text = stu(1).na Text1(2).Text = stu(1).ch Text1(3).Text = stu(1).en Text1(4).Text = stu(1).ma End Sub 编写列表框List1的Click事件代码: Private Sub List1_Click() n = List1.ListIndex + 1 Text1(0).Text = stu(n).no Text1(1).Text = stu(n).na

  26. Text1(2).Text = stu(n).ch Text1(3).Text = stu(n).en Text1(4).Text = stu(n).ma End Sub 另外编写文本框组的事件代码,使之方便输入: Private Sub Text1_GotFocus(Index As Integer) Text1(Index).SelStart = 0 Text1(Index).SelLength = Len(Text1(Index).Text) End Sub Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer) If KeyAscii = 13 Then i = IIf(Index = 4, 0, Index + 1) Text1(i).SetFocus End If End Sub

  27. 习题八 8.1 在习题4.9中利用文本框检查用户口令的程序中,使用静态变量来限制输入口令的次数。 8.2 在标准模块中编写求最大公约数的Function过程,然后在窗体模块中调用,来对分数进行化简,如图8-15所示。

  28. 8.3 为例8-9增加计算全班平均成绩的功能。 8.4 为例8-9增加按某科成绩排序的功能。 8.5 为例8-9增加按姓名查找的功能。 8.6 “井字棋游戏”,其规则是:轮流出棋,首先三子连成线者为赢。 8.7 “扫雷”游戏是Windows中最著名的游戏,请设计一个简单的扫雷游戏(提示:在设计过程中,不仅需要用到顺序、选择、循环结构,而且还用到递归算法)。

More Related