AN EXAMPLE: 'add a commandbutton whose name is "newbutton"and index is 0,visiable is false Option Explicit Private Sub Form_Click() addcmdline End Sub Sub addcmdline() Me.WindowState = 2 On Error Resume Next Dim i As Long For i = 0 To 99 Load newbutton(i) newbutton(i).Left = (i Mod 10) * 1000 + 1050 newbutton(i).Top = (i \ 10) * 500 + 550 newbutton(i).Caption = "cmd" & i newbutton(i).Width = 900 newbutton(i).Height = 400 newbutton(i).Visible = True Next Me.ForeColor = vbRed Me.DrawWidth = 3 For i = 0 To 10 Me.Line (1000, 500 + i * 500)-(11000, 500 + 500 * i) Me.Line (1000 + i * 1000, 500)-(1000 + 1000 * i, 5500) Next End SubPrivate Sub newbutton_Click(Index As Integer) MsgBox " You have clicked " & newbutton(Index).Caption, 64 End Sub
private sub command1_click()
static MaxIdx
if MaxIdx = 0 then MaxIdx = 1
MaxIdx = MaxIdx + 1
load optButton(MaxIdx) '建立新的控件数组
optButton(MaxIdx).top = optButton(MaxIdx - 1).top + 360
optButton(MaxIdx).visible = true '是新的控件数组可见
end sub
object 在控件数组中添加或删除的控件名称。
index% 控件在数组中的索引值。
加载控件数组的新元素时,大多数属性设置值将由数组中具有最小下标的现有元素复制─ 本例中是索引值为 0 的元素。因为不会自动把 Visible、Index 和 TabIndex 属性设置值复制到控件数组的新元素中,所以,为了使新添加的控件可见,必须将其 Visible 属性设置为 True。注意 试图对数组中已存在的索引值使用 Load 语句时,Visual Basic 将生成一个错误。重点 可用 Unload 语句删除所有由 Load 语句创建的控件,然而,Unload 无法删除设计时创建的控件,无论它们是否是控件数组的一部分。
Option Explicit
Private Sub Form_Click()
addcmdline
End Sub
Sub addcmdline()
Me.WindowState = 2
On Error Resume Next
Dim i As Long
For i = 0 To 99
Load newbutton(i)
newbutton(i).Left = (i Mod 10) * 1000 + 1050
newbutton(i).Top = (i \ 10) * 500 + 550
newbutton(i).Caption = "cmd" & i
newbutton(i).Width = 900
newbutton(i).Height = 400
newbutton(i).Visible = True
Next
Me.ForeColor = vbRed
Me.DrawWidth = 3
For i = 0 To 10
Me.Line (1000, 500 + i * 500)-(11000, 500 + 500 * i)
Me.Line (1000 + i * 1000, 500)-(1000 + 1000 * i, 5500)
Next
End SubPrivate Sub newbutton_Click(Index As Integer)
MsgBox " You have clicked " & newbutton(Index).Caption, 64
End Sub
optButton(MaxIdx).top = optButton(MaxIdx - 1).top + 360
optButton(MaxIdx).visible = true