好文档 - 专业文书写作范文服务资料分享网站

VB实例源码

天下 分享 时间: 加入收藏 我要投稿 点赞

Print \您输入的矩阵是:\Print

For i = 1 To n

Print Tab(8); ‘打印位置定位 For j = 1 To m Print arr(i, j); Next j

Print ‘换行 Next i

Print

Print \矩阵中最大值为:\其位置在第\行\& col & \列\End Sub 第6章 课前体验

(2)编写窗体装载事件过程:

Private Sub Form_Load() For I = 0 To 100 List1.AddItem i Next i End Sub

(3)使用“添加过程”对话框创建出判断成绩等级的过程

Public Sub DengJi(a As Integer) Select Case a Case 0 To 59

Label1.Caption = \不及格\ Case 60 To 69

Label1.Caption = \及格\ Case 70 To 79

Label1.Caption = \中等\ Case 80 To 89

Label1.Caption = \良好\ Case 90 To 100

Label1.Caption = \优秀\ End Select End Sub

4)编写列表框单击Click事件过程: Private Sub List1_Click() Dim n As Integer N = Val(List1.Text)

Call DengJi(n) '调用判断成绩等级的过程 End Sub

(5)编写命令按钮单击Click事件过程: Private Sub Command1_Click()

11

End End Sub

【例6-1】

Private Sub oushu(x As Integer,msg As Boolean) If x Mod 2 = 0 Then Msg = True Else

Msg = False End If End Sub 【例6-2】

Public Function Rect(a As Double,b As Double)As Double Rect = a * b End Function 【例6-3】

(2)在窗体代码窗口中编写pingjun过程:

Sub pingjun(a As Integer,b As Integer,c As Integer) C = (a+b)/2 End Sub

(3)编写命令按钮1的单击事件过程:

Private Sub Command1_Click()

Dim x As Integer,y As Integer,z As Integer X = Val(Text1.Text) Y = Val(Text2.Text)

Call pingjun(x,y,z) '或pingjun x, y, z Text3.Text = Str(z)

End Sub 【例6-4】

(2)把判断奇偶性的函数过程jo的程序代码输入到窗体代码窗口中。Function jo(x As Integer)As String If x Mod 2 = 0 Then Jo = \偶数\ Else

Jo = \奇数\ End If End Function

(3)编写命令按钮1的单击事件过程:

Private Sub Command1_Click() Dim n As Integer,w As String n=Val(Text1.Text) w = jo(n)

Label1.Caption=Text1.Text &\是\!\

12

End Sub

【例6-5】

(2)编写参数按地址传递次的过程Swap1:

Sub Swap1(x As String,y As String) Dim t As String T = x:x = y:y = t

End Sub

(3)编写命令按钮单击事件过程如下:

Private Sub Command1_Click() Dim a As String,b As String A = Text1.Text B = Text2.Text

Form1.Caption = \按地址传递\ Swap1 a,b Text1.Text = a Text2.Text = b End Sub 【例6-7】

(2)求任意一维数组中各元素之积的函数如下:

Function tt(a() As Integer)As Long '函数的形参是数组 Dim t#, i% T = 1

For I = LBound(a) To UBound(a) '求数组的下界和上界 T = t * a(i) Next i Tt = t

End Function

(3)求任意一维数组中各元素之和的函数如下:

Function ss(b() As Integer)As Long '函数的形参是数组 Dim t#, i% S = 0

For I = LBound(b) To UBound(b) '求数组的下界和上界 S = s+b(i) Next i Ss = s

End Function

(4)编写命令按钮单击事件过程,如下:

Private Sub Command1_Click() Dim a(1 To 5)As Integer Dim b(2 To 10)As Integer

Dim i As Integer,t1 As Long,s1 As Long For I = 1 To 5 '给数组赋值

13

a(i) = i + 3 Next i

t1 = tt(a()) '调用函数

Print\第一个数组各元素之积t1=\ For I = 2 To 10 b(i) = i * 2 Next i

s1 = ss(b())

Print\第二个数组各元素之和s1=\End Sub 【例6-8】

(1)先定义一个具有可选参数的函数过程sum,用来进行3个数的加法运算。Private Function sum(x As Integer,Optional y As Integer,_

Optional z As Integer=3)As Integer Sum = x + y + z End Function

(2)编写窗体的单击事件过程,如下:

Private Sub Form_Click()

Print\ '省略两个参数 Print\ '省略第3个参数 Print\ '省略第2个参数 Print\ '不省略参数 End Sub 【例6-9】

(1)先定义一个具有可变参数的函数过程MySum,如下: Function MySum(ParamArray VA()) As Integer '声明为可变参数 Dim i As Integer Dim Sum As Integer Sum = 0

For i=LBound(VA) To UBound(VA) '得到数组的大小,并进行循环 Sum = Sum+VA(i) Next

MySum = Sum End Function

(2)编写窗体的单击事件过程,如下: Private Sub Form_Click() Dim s As Integer Print

Print Tab(2); \使用3个实参:\

S = MySum(2,4,6) ' 可以使用任意多个实参来调用 Print \ Print

Print Tab(2); \使用5个实参:\

14

S = MySum(1,2,3,4,5) Print \End Sub

【例6-10】

(1)定义具有窗体参数的过程,如下:

Private Sub BiaoTi(fm As Form) 'fm为窗体对象参数 Text1.Text = \窗体的标题是\End Sub

调用过程BiaoTi会改变窗体对象fm中的文本框Text1的文本。 (2)定义具有控件对象参数的过程,如下:

Private Sub KuanDu(tb As TextBox) 'tb为文本框型的控件对象参数 tb.Text = \文本框的宽度是\End Sub

调用过程KuanDu会改变文本框对象tb的文本。

(3)编写按钮的单击事件过程,用来调用具有对象参数的过程。

Private Sub Command1_Click() Call BiaoTi(Form1) End Sub

Private Sub Command2_Click() Call KuanDu(Text1) End Sub 【例6-11】

Function fac(n As Integer) As Long If n = 1 Then Fac = 1 Else

Fac = n * fac(n - 1) End If End Function 【例6-12】

Private Sub Command1_Click()

Dim a As Integer,b As Integer '过程级变量 A = 100:b = 8 Print

Print\调用s1前,事件过程中的变量:\ Call s1 '调用通用过程sub1 Print

Print\调用s1后,事件过程中的变量:\End Sub

Sub s1() '通用过程

Dim a As Integer,b As Integer '过程级变量 A = 55:b = 66

15

VB实例源码

Print\您输入的矩阵是:\PrintFori=1TonPrintTab(8);‘打印位置定位Forj=1TomPrintarr(i,j);NextjPrint
推荐度:
点击下载文档文档为doc格式
0rosj0d4ij47hq710ehk
领取福利

微信扫码领取福利

微信扫码分享