龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > VB开发 >

控件数组的操作技巧(3)

时间:2009-12-30 15:42来源:未知 作者:admin 点击:
分享到:
Private Sub mnuFiles_Click(Index As Integer) Dim ret As String 用Windows的记事本打开文件:注意中间的空格不能少 ret = "Notepad.exe" " " MyPath mnuFiles(Index).Caption Shell ret, vbNor

Private Sub mnuFiles_Click(Index As Integer)

Dim ret As String
'用Windows的记事本打开文件:注意中间的空格不能少
ret = "Notepad.exe" & " " & MyPath & mnuFiles(Index).Caption
Shell ret, vbNormalFocus

End Sub

四、控件在窗体上的排列问题

曾有一位网友两次问土人怎样才能在窗体上添加100万个文本框。呵呵,这要多大的显示器才能显示出来呀!不过,添加的数组控件多了,排列确实是个问题。下面的例子给窗体添加499个Label控件,连同事先绘制好的Index号定义为0的控件,总共有500个,能在窗体上从左到右、从上到下整齐地排列。程序运行时要花些许时间。

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

精彩图集

赞助商链接