一.运行中设置数组控件的属性
设窗体上有若干个以 Command1 命名的命令按钮,现要求:点击其中一个按钮后,该按钮不可用,而其它的按钮均可用。以下几行代码可以实现这个要求,比一个一个的设置高效得多。
[html] view plaincopy
<strong><span style="font-size:18px;">Private Sub Command1_Click(Index As Integer)
Dim i As Integer '计数器
Dim comNum As Integer '按钮的索引号
comNum = 0
For i = 0 To Command1.Count - 1
comNum = comNum + 1
If comNum > Command1.Count - 1 Then comNum = 0
Command1(comNum).Enabled = True '让所有按钮可用
Next
Command1(Index).Enabled = False '让被单击按钮不可用
End Sub</span></strong>
二、运行中添加和卸载数组控件
窗体上已有一个文本框Text1,程序需要在运行时动态地创建若干文本框,可这样实现:
1.首先,设计时给Text1的Index属性设置为"0",这一步很重要:有了索引号才能创建数组控件;
2.编写代码:[之前请给工程添加两个命令按钮,Name属性取缺省值,Caption属性分别为:添加、卸载]
[html] view plaincopy
<strong><span style="font-size:18px;">Private Sub Command1_Click()
Dim txtNum As Integer 'Text1的Index号
Dim Num As Integer '赋给各TextBox的值
txtNum = 0 '初值
Num = 1 '初值
Text1(0).Text = "Text" & Num '第一个Text1的值
Dim i As Integer '计数器
For i = 0 To 4 '添加五个TextBox
txtNum = txtNum + 1
Num = Num + 1
Load Text1(txtNum) '加载文本框
Text1(txtNum).Top = Text1(txtNum - 1).Top + 450 '设置位置
Text1(txtNum).Text = "Text" & Num '加载内容
Text1(txtNum).Visible = True '令其可见:不能漏
Next
Command1.Enabled = False
Command2.Enabled = True
End Sub
Private Sub Command2_Click()
Dim i As Integer, N As Integer
N = 0
For i = 1 To Text1.Count - 1
N = N + 1
Unload Text1(N)
Next
Command1.Enabled = True
Command2.Enabled = False
End Sub
</span></strong>
三、给运行中添加的数组控件命名并编写事件
其实例二已经给数组控件命了名,只是简单了些。本例将文件夹"F:\Txt"下的所有.txt的文件名作为菜单的Caption添加到菜单项里[请事先建立此文件夹并拷贝一些.txt文件,或者更改代码],当点击这些菜单,程序将调出Windows的记事本打开相应的文件。
键入以下代码前请启动VB的菜单编辑器,建立一个父菜单项[Naem和Caption属性任意],然后在此父菜单下建立一个子菜单,Name属性为"mnuFiles",Caption属性为"没有文件"。
[html] view plaincopy
<strong><span style="font-size:18px;">Dim MyPath As String '路径名变量
'添加并命名菜单过程
Private Sub AddMenu()
Dim MenuNum As Integer '菜单索引号变量
Dim MyFiles As String '文件名变量
MenuNum = 0 '初值
MyFiles = Dir(MyPath + "*.txt") '仅查找文本文档
If MyFiles = "" Then Exit Sub '为空则退出过程
mnuFiles(0).Caption = MyFiles '给第一个菜单命名
Do Until MyFiles = ""
MyFiles = Dir()
If MenuNum = 0 Then MenuNum = 1
MenuNum = MenuNum + 1
Load mnuFiles(MenuNum) '加载菜单
mnuFiles(MenuNum).Visible = True '设置为可见:不能漏
mnuFiles(MenuNum).Caption = MyFiles
If mnuFiles(MenuNum).Caption = "" Then Unload mnuFiles(MenuNum)
'若菜单名为空则卸载掉
Loop
End Sub
Private Sub Form_Load()
MyPath = "F:\Txt\" '规定路径
Call AddMenu '执行添加菜单过程
End Sub
Private Sub mnuFiles_Click(Index As Integer)
Dim ret As String
'用Windows的记事本打开文件:注意中间的空格不能少
ret = "Notepad.exe" & " " & MyPath & mnuFiles(Index).Caption
Shell ret, vbNormalFocus
End Sub
</span></strong>
四、控件在窗体上的排列问题
当然添加的数组控件多了,排列确实是个问题。下面的例子给窗体添加499个Label控件,加上事先绘制好的Index号定义为0的控件,总共有500个,能在窗体上从左到右、从上到下整齐地排列。程序运行时要花些许时间。
[html] view plaincopy
<strong><span style="font-size:18px;">Option Explicit
Private Sub Form_Load()
Me.Width = 6800
Me.Height = 6000
Me.Caption = "添加标签演示"
With Label1(0)
.Top = 0
.Left = 120
.Height = 200
.Width = 300
.Caption = ""
End With
AddLabels
End Sub
Private Sub AddLabels()
Dim i As Integer '计数器
Dim Num As Integer 'Label的Index号
Dim k As Integer '判断是否满行
Dim n As Integer '行数计数器
Num = 0
k = 0
n = 0
Label1(0).Caption = Num + 1
For i = 1 To 499
Num = Num + 1
Load Label1(Num) '加载Label控件
Select Case k
Case 0
Label1(Num).Left = Label1(Num - 1).Left + Label1(0).Width + 20
If Num = 20 Then n = n + 1: k = n
Case n
Label1(n * 20).Left = 120
Label1(n * 20).Top = Label1(0).Height * n
Label1(Num).Top = Label1(0).Height * n
Label1(Num).Left = Label1(Num - 1).Left + Label1(0).Width + 20
If Num = (n + 1) * 20 Then n = n + 1: k = n
End Select
Label1(Num).Caption = Num + 1
Label1(Num).Visible = True
Next
End Sub