Visul Basic 程序设计课件chapter6(精选)
Visual Basic 程序设计教程第6章

其格式为: Erase数组名[, 数组名]„„ Erase语句用来重新初化静态数组的各 元素,或释放动态数组的存储空间。注意: 在Erase语句中,只给出需要刷新的数组名, 不带括号和下标。例如:Erase a。
【说明】 (1)当把Erase语句用于静态数组时, 如果这个数组是数值型数组,则把数组中 各元素置为0;如果是字符串数组,则把所 有各元素置为空字符串;如果是记录数组, 则根据每个元素(包括定长字符串)的类 型重新进行设置。设置如表6.2所示:
4.数组的操作
(1).数组元素的输入和输出
数组元素一般通过过循环语句、文本 框控件、InputBox( )函数配合进行输入和 输出。
(2).数组的清除和重新定义
静态数组一经定义,编译时便在内存 中分配了相应的存储空间,从建立数组到 程序运行结束,它的大小和维数是不能改 变的;而动态数组是在程序的执行过程序 中用ReDim语句根据用户的需要重新分配存 储空间的。可能需要清除数组的内容或对 数组重新定义,这时可以用Erase语句来实 现。
(3).数组元素的引用
若要将数组的各个元素的值赋给另一 个数组,在VB 6.0以前的版本中,需要通 过for„next循环来实现。在VB 6.0中只要 通过一句简单的赋值语句即可实现。
在数组对数组赋值时要注意:
①赋值号“=”两边的数据类型必须 一致; ②如果赋值号左边是一个动态数组, 则赋值时系统自动将动态数组ReDim 成与右边同样大小的数组; ③如果赋值号“=”左边是一个大小 固定的数组,则数组赋值出错。
(2)当把Erase语句作用于动态数组时, 将删除整个数组结构并释放该数组所占用 的内存空间。也就是说,动态数组经Erase 后即不复存在,而静态数组经Erase后仍然 存在,只是其内容被清空。
《Visual Basic程序设计实用教程》 第6章

Print an(i), an(i + 3) Next i End Sub
2020/1/11
控件数组
控件数组由一组相同类型的控件组成。这些控件具
有相同的名称,具有很多相同的属性。数组中的每个 控件都有唯一的索引号,即下标,下标值由Index属性 指定,第1个控件数组元素的下标为0,第2个控件数组 元素的下标为1等等。它是创建控件数组时系统自动按
顺序赋给每个控件数组元素的,程序通过索引值来区 别控件数组中的元素。
定义可调数组的方法是 (1) 用Dim语句(或Private和Public),但不要指定维数。 (2) 再用ReDim语句指定数组的准确尺寸。
2020/1/11
例6.6计算前三名学生成绩。设计一个窗体,在窗体上添加两 个命令按钮。运行程序时,单击【计算】,弹出输入对话框, 要求输入学生人数。以后要求输入每个学生的成绩,输入结 束后计算并输出学生人数和平均成绩,然后再输出成绩最高 的前三名学生的成绩。
2020/1/11
在计算机中数组占据一块内存区域,数组名是这个区域的名 称,下标可标识数组元素在该区域的位置。数组应遵循先定 义后使用的原则。定义数组的目的是为其留出所需空间。这 一点和控件属性中使用到的数组不同,控件属性中用到的数 组是系统定义的。 定义数组的一般格式: Dim 数组名(第一维说明[,第二维说明] …..) [As 类型]
visual basic chapter6

Chapter 6 –Repetition 6.1 Do Loops6.2 For...Next Loops6.3 List Boxes and LoopsDo Loops•Pretest Form of a Do Loop•Posttest Form of a Do Loop6.1 Do Loops•A loop is one of the most important structures in programming.•Used to repeat a sequence of statements a number of times.•The Do loop repeats a sequence of statements either as long as or until acertain condition is true.Pretext Do LoopDo While conditionstatement(s)Loop Condition is tested, If it is true,the loop is run.If it is false, the statementsfollowing the Loop statementare executed.These statements are inside the body of the loop andare run if the conditionabove is true.Pseudocodeand Flow ChartExample 1Private Sub btnDisplay_Click(...) _Handles btnDisplay.Click 'Display the numbers from 1 to 7Dim num As Integer= 1Do While num <= 7lstNumbers.Items.Add(num)num += 1 'Add 1 to the value of num LoopEnd SubExample: Repeat Request asLong as Response in IncorrectDim passWord As String= ""Do While passWord <> "SHAZAM"passWord = InputBox("What is the password?") passWord = passWord.ToUpperLooppassWord is the loop controlvariable because the value storedin passWord is what is tested todetermine if the loop shouldcontinue or stop.Example 3: Sentinel-Controlled LoopDim num As Double = 0Dim prompt As String = "Enter a nonnegative number. Enter -1 "&"to terminate entering numbers."num = CDbl(InputBox(prompt))Do While num <> -1 '-1 is sentinel value ..num = CDbl(InputBox(prompt))LoopPosttest Do LoopDostatement(s)Loop Until conditionLoop is executed once and then the condition is tested. If it is false, the loop is run again.If it is true, the statements following the Loop statement are executed.Example: Repeat RequestUntil Proper ResponseDim passWord As String = ""DopassWord = InputBox("What is the password?") passWord = passWord.ToUpperLoop Until passWord = "SHAZAM"Pseudocodeand FlowchartExample 5: FormtxtAmounttxtWhenExample 5: CodePrivate Sub btnCalculate_Click(...) Handles _btnCalculate.Click Dim balance As Double, numYears As Integer balance = CDbl(txtAmount.Text)Do While balance < 1000000balance += 0.06 * balancenumYears += 1LooptxtWhen.Text = "In " & numYears &" years you will have a million dollars." End SubExample 5: OutputComments•Be careful to avoid infinite loops –loops that never end.•Visual Basic allows for the use of either the While keyword or the Until keywordat the top or the bottom of a loop.6.2 For…Next Loops •General Form of a For…Next Loop •Nested For…Next Loops•Local Type InferenceFor…Next Loops•Used when we know how many times we want the loop to execute•A counter controlled loopFor…Next Loop SyntaxSampleFor i As Integer = 1 To5lstTable.Items.Add(i & " "& i ^ 2)NextThe loop counter variable, i, is•initialized to 1•tested against the stop value, 5•incremented by 1 at the Next statementDim i As Integer= 1Do While i <= 5lstTable.Items.Add(i & " "& i ^ 2)i += 1LoopExample 1: OutputDim pop As Double = 300000For yr As Integer = 2010 To2014lstTable.Items.Add(yr & " "&FormatNumber(pop, 0)) pop += 0.03 * popNextStep Clause•Normally after each pass the value of the counter variable increases by 1•If the clause Step s is appended to the For statement, the value of s will be added to the counter variable after each pass.•If the value of s is a negative number, the value of the counter variable will decrease after each pass.Example with Negative StepValueFor j As Integer = 10 To 1 Step-1 lstBox.Items.Add(j)NextlstBox.Items.Add("Blastoff")Example: Nested For…NextLoopsFor i As Integer = 65 To70For j As Integer= 1 To25lstBox.Items.Add(Chr(i) & j)NextNextOutput:A1A2A3:InnerloopOuterloopFor and Next Pairs•For and Next statements must be paired. •If one is missing, the automatic syntax checker will complain with a wavy underline and a message such as“A ‘For’ must be paired with a ‘Next’.”Start, Stop, and Step values •Consider a loop beginning withFor i As Integer= m To n Step s•The loop will be executed exactly once if m equals n no matter what value s has. •The loop will not be executed at all if m is greater than n and s is positive,or if m is less than n and s is negative.Altering the Counter Variable•The value of the counter variable should not be altered within the body of the loop. •Doing so might cause the loop to repeat indefinitely or have an unpredictablenumber of repetitions.Non-Integer Step Values •Can lead to round-off errors with the result that the loop is not executed the intended number of times.•We will only use Integers for all values inthe header.6.3 List Boxes and Loops •Some Properties, Methods, and Events of List Boxes•List Boxes Populated with Strings •List Boxes Populated with Numbers•Searching an Ordered ListList Box Properties•The total number of items in a list box is lstBox.Items.Count•Note:Each item in lstBox is identified by an index number from 0 to lstBox.Items.Count –1•The index number of the currently highlighted item is given by:lstBox.SelectedIndexMore List Box Properties•lstBox.Items() is the list of items in the list box. •The value of the item with an index of n is: lstBox.Items(n)•The data type of the elements in thelstBox.Items() array is Object. To display the first element of lstBox.Items in a text box: txtBox.Text = CStr(lstBox.Items(0))Currently Highlighted Item ina List BoxesThe value of the currently highlighted item as a string can be obtained aslstBox.TextThe Sorted Property•Items can be placed into the list at design time or run time•The Sorted property causes items in the list to be sorted automatically as strings •Caution: Numbers in a sorted list box will not necessarily be in increasingnumerical orderExample 1: FormlstStateslstStates is filled at design time with the names of the U.S. states in the order they joined the union.Private Sub btnDisplay_Click(...) Handles_btnDisplay.Click 'Display last ten states to join the unionDim n As Integer= lstStates.Items.CountFor i As Integer = (n -1) To(n -10) Step-1 lstLastTen.Items.Add(lstStates.Items(i)) NextEnd SubExample 1: OutputExample: FormlstAgeslstAges is filled at design time with the ages of the U.S. presidents when taking office.txtAvgAgeExample: CodePrivate Sub btnCalculate_Click(...) Handles_btnCalculate.Click 'Calculate average age of presidents whenassuming officeDim n As Integer = lstAges.Items.CountDim sum As Double = 0For i As Integer = 0 To n –1sum += CDbl(lstAges.Items(i))NexttxtAvgAge.Text = FormatNumber(sum / n, 2) End SubExample: OutputSearching a List of Strings•A search often can be terminated earlier if the list is sorted.•This is particularly the case when the sought-after item is not in the list. The search can be terminated when an item is reached that follows the sought-afteritem alphabetically.Flags•A flag is a variable that keeps track of whether a certain situation has occurred.•The data type most suited to flags isBoolean.Searching an Unsorted List•A flag is used to indicate whether or not the sought-after item has been found.•The flag variable is initially set to False and then set to True if and when the itemis found.More About FlagsWhen flagVar is a variable of Boolean type, the statementsIf flagVar= True ThenandIf flagVar= False Thencan be replaced byIf flagVar ThenandIf Not flagVar ThenMore About Flags (continued)The statementsDo While flagVar = TrueandDo While flagVar = Falsecan be replaced byDo While flagVarandDo While Not flagVarCounters and Accumulators•A counter is a numeric variable that keeps track of the number of items that have been processed.•An accumulator is a numeric variablethat totals numbers.Nested LoopsStatements inside a loop can containanother loop.。
Visual Basic程序设计简明教程 第6章

本章内容:
2019/3/6
1
一维数组 二维数组 动态数组 控件数组 自定义类型 字符串的处理 列表框 组合框
6.1 一维数组
一维数组的定义方式:
Dim 数组名([下界 To]上界) As 类型
例如: Dim a(1 To 5) As Integer 数组的元素在内存中按顺序存放,数组所 占据的字节数是各元素所占字节数之和。
9
2019/3/6
《Visual Basic程序设计简明教程》
例6.4
求两个3×3矩阵的和。
Private Sub Command1_Click() Const N As Integer = 3 Dim a(1 To N, 1 To N) As Integer, b(1 To N, 1 To N) As Integer Dim c(1 To N, 1 To N) As Integer, i As Integer, j As Integer For i = 1 To N For j = 1 To N a(i, j) = Val(InputBox("输入a(" & i & "," & j & ")")) '输入数据存入数组a Next j Next i MsgBox ("矩阵A的数据输入完毕!") For i = 1 To N For j = 1 To N b(i, j) = Val(InputBox("输入b(" & i & "," & j & ")")) '输入数据存入数组b Next j Next i MsgBox ("矩阵B的数据输入完毕!")
Unit6Visual Basic程序设计(PPT全)(钱显鸣版)

循环次数
n = Int((终值-初值)/步长 + 1)
例:
计算:1 + 2 + 3 + …… + n
(n=10)
For i = 1 To 10 s=s+i Next i
计算:
n =10
n! = 1!+2!+ + 10! ∑
n =1
t=1 For n = 1 To 10 t=t*n s=s+t Next n
循环的嵌套
在一个循环体内又包含了完整的循环结构
使用循环语句要注意以下几点
内循环控制变量与外循环控制变量不能同名; 外循环必须完全包含内循环,不能交叉;
若循环体内有If语句,或If语句内有循环语句,不能交
叉; 利用GoTo语句可以从循环体转向循环体外,但不能从 循环外转入循环体内。
内外循环交叉
For i = 1 To 4 For j = 1 To 5 …… Next i Next j 错误
循环结构与If语句结构交叉
For i= 1 To 5 If 表达式 Then …… Next i End If
错误
Space与strDup 函数
Space(N)
返回有N个空格组成的字符串。
第四章
基本控制结构(三)
教学目标
掌握For…Next、 Do…Loop循环语句
掌握滚动条控件的使用
For循环语句——计数型循环语句
For 循环变量 = 初值 To 终值 [Step 步长] 循环体 Next [循环变量] 说明: 循环变量:数值型 初值、终值:数值型 步长:数值型,缺省时步长为1
滚动条
Visual Basic程序设计(第三版) 第6章

5
VB的代码模块 VB的应用程序是由过程组成的,过程代码放在模块中。 VB提供了三类代码模块: 窗体模块、标准模块和类模块 模块管理是通过工程资源管理器窗口来组织和管 理一个工程(应用程序)的。VB模块及应用程序的过 程之间的关系如下图所示:
VB应用程序 .VBP
窗体模块 .frm
类模块 .cls
说明:(1) 实参表与过程定义时形参列表的个数,相同,类 型对应相同,参数名可不同.
(2) 用call或直接调用时,若无实参,过程名后可省略,若 有实参则call调用实参则写入圆括号内,而直接调 用的,实参不得使用圆括号.
10
.(3)
(3) 若有两个以模块包括同名过程,或被调过程是在
窗体模块中定义的,在调用时必须用模块名来加以 限定。被调过程是在标准模块中定义,并且过程 名唯一,则可省略模块名。
函数: <变量名>=<函数名>[<实参表>] 注: 若函数无需返回值时, 也可用call调用
19
6.5 过程之间参数的传递
6.5.1 形参与实参
实参与形参的对应关系:在定义过程时,形参为实参预 留位置;在调用时实参的值被一一插入到对应的形参位 置上去。第一个形参接收第一个实参的值,第二个形 参接收第二个实参的值……
end sub (2) 使用“添加 过程”对话框建立。
8
•先打开要添加过程的模 块代码编辑器窗口; •选择“工具”菜单中的 “添加过程”命令; •在对话框中的名称文本 框中输入过程名并选择 相应的类型和范围; •单击“确定”按钮后即 可在代码编辑窗口中输 入过程的代码。
6.3.2. 通用过程的调用
Dim x As Integer, y As Integer, z As Integer
visual basic程序设计基础全套教程、教案第六章过程ppt课件_图文
136.1 函数过程的定义和用146.2 子过程的定义和调用
• (2)程序执行函数过程Area,计算出内圆的面积,执行到End Function语句时, 函数返回到事件过程的中断处,返回值通过语句①赋值给变量s1。
• (3)程序执行语句②,第二次调用函数Area,事件过程再次中断,系统记录本 次中断地址,程序转向执行函数Area,同时实参向形参传递外径r2的相关信息。
• (7)Exit Function语句是可选的,使执行立即从函数过程中退出, 程序接着从调用该函数过程的语句之后的代码执行。在函数过程的 任何位置都可以有 Exit Function 语句。
• (8)函数过程有一个返回值并具有数据类型,因此适用于完成某种 计算,或通过处理获得一个计算结果的情况。
9
6.1 函数过程的定义和调用
•6.1.3 函数过程的调用 • 1.函数过程的调用形式 • 函数过程名([实参列表])
• 有参数的函数过程调用,过程名后面的括号不能省略;无参数的函 数过程调用,过程名后面的括号可省略也可保留。
• 由于函数过程有返回值,函数过程的调用不能作为单独的语句使用, 必须作为表达式或表达式的一部分,再配以其他的语法成分构成语 句。
r2
r1
4
6.1 函数过程的定义和调用
5
6.1 函数过程的定义和调用
• 可以看出,在VB应用程序中使用自定义过程可以提高用户编写程序 的效率,使程序简练、便于调试和维护。
VB程序设计第六章
rem 画三角形
Line (10, 30)-(10, 80), vbRed '(10,30)-(10,80)
Line -(40, 80), vbGreen '(10,80)-(40,80)
Line -(10, 30), vb,30)
rem 画矩形
Line (50, 30)-(80, 80), vbRed, BF
' 自定义坐标
DrawWidth = 8
'设置点的大小
m_x = Rnd * 100 m_y = Rnd * 100
'设置随即坐标m_x,m_y
m_red = Rnd * 255 m_green = Rnd * 255 m_blue = Rnd * 255
'设置随机颜色
PSet (m_x, m_y), RGB(m_red, m_green, m_blue) '画点
2020/2/7
6.1 图形控件
6.1.3 图形方法
2、画直线、矩形方法 例[6-1_7]用Line方法画三角形和矩形。 解题思路:画三角形:前一条直线的终点就是后一条直线的起点 ,依次画完
三条线就构成三角形。
rem 自定义坐标系,左上角(0,0),右下角(100,100)
Scale (0, 0)-(100, 100) DrawWidth = 5
2020/2/7
6.1 图形控件
6.1.3 图形方法
3、画圆方法 Circle方法用于画圆、椭圆、圆弧和扇形; 格式:
[对象名].Circle[Step](x,y),radius,[color],[start],[end][,aspect]
其中: 对象名:窗体或图片框; Step:可选项,有该参数时表示坐标为相对于当前点的坐标,否则 为绝对坐标。 (x,y):中心坐标 Radius:半径 Color:边框颜色,若省略则使用ForeColor属性指定的颜色; Start,End:指定弧的起始、终止位置,以弧度为单位, 取值:-2π~2π;负号表示在画弧的同时,还画出圆心到弧的 的端点的连线; Aspect:表示纵轴和横轴的尺寸比,Aspect<1,则表示在x轴方向画 椭圆,Aspect>1,表示在y轴方向画椭圆,默认为1;
Visual Basic程序设计教程第6章
主要方法:
Cls Print
4
6.2.2 图形文件的装入
1.图片框与图像框的区别 2.在设计阶段装入图形文件
用属性窗口中的Picture属性装入 利用剪贴板把图形粘贴到窗体、图片框或图像框中
9
6.5.2 组合框
1.组合框属性:
列表框的属性基本上都可用于组合框 Style Text
2.组合框事件 3.组合框方法
与列表框相同
10
6.6 滚动条
主要属性:
Max Min LargeChange Value
SmallChange
主要事件:
Scroll Change
6.9.1 设置焦点
在运行时单击对象 运行时用快捷键选择对象 在程序代码中使用SetFocus方法
6.9.2
Tab顺序
用Tab键也可以把焦点移到某个控件中。每按一次Tab 键,可以使焦点从一个控件移到另一个控件。Tab顺序 就是焦点在各个控件之间移动的顺序。
14
6.框和组合框
6.5.1
列表框
Columns List Listcount ListIndex Multiselect Selected Style Text
主要属性:
主要方法:
AddItem Clear RemoveItem
11
6.7 计时器
主要属性:
Interval Enabled
主要事件:
Timer
12
6.8 框架
框架用来对控件进行分组,即把指定的控件放到框
Visual Basic程序设计VB第6章电子讲稿
6.1 数组应用案例
For i = 1 To 9 For j = i + 1 To 10 If a(i) > a(j) Then t = a(i): a(i) = a(j): a(j) = t '交换位置 End If Next j Next i Print "排序结果:" For j = 1 To 10 Print a(j); Next j End Sub 上述程序代码中,中间程序段“For i = 1 To 9”~“Next i”(共7 个程序行)用于实现数据的排序。也可以把这个程序段改写为
6.1 数组应用案例
a(1, 1) = 69: a(1, 2) = a(2, 1) = 94: a(2, 2) = a(3, 1) = 57: a(3, 2) = a(4, 1) = 98: a(4, 2) = a(5, 1) = 73: a(5, 2) = Show Print "学生", "平均分" Print String(20, "-") '输出20个减号“-” For r = 1 To 5 s = 0 累加前清0 For c = 1 To 3 s = s + a(r, c) '累加同一行数据 Next c 89: 80: 62: 94: 76: a(1, a(2, a(3, a(4, a(5, 3) 3) 3) 3) 3) = = = = = 74 90 73 90 63
'在模块级声明段中声明 'score用做数组变量名
ቤተ መጻሕፍቲ ባይዱ
Label1.Caption = "单击“查找”按钮开始查找最高分和最低分" score = Array(89, 96, 81, 67, 79, 90, 63, 85, 95, 83) End Sub