怎样用代码创建控件数组?谢谢

解决方案 »

  1.   

    刚刚看了一下书,有个例子:
    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
      

  2.   

    运行时添加控件数组在运行时,可用 Load 和 Unload 语句添加和删除控件数组中的控件,然而,添加的控件必须是现有控件数组的元素。必须在设计时创建一个(在大多数情况下)Index 属性为 0 的控件,然后在运行时使用如下语法:Load object(index%)Unload object(index%)参数 描述 
    object 在控件数组中添加或删除的控件名称。 
    index% 控件在数组中的索引值。 
    加载控件数组的新元素时,大多数属性设置值将由数组中具有最小下标的现有元素复制─ 本例中是索引值为 0 的元素。因为不会自动把 Visible、Index 和 TabIndex 属性设置值复制到控件数组的新元素中,所以,为了使新添加的控件可见,必须将其 Visible 属性设置为 True。注意 试图对数组中已存在的索引值使用 Load 语句时,Visual Basic 将生成一个错误。重点 可用 Unload 语句删除所有由 Load 语句创建的控件,然而,Unload 无法删除设计时创建的控件,无论它们是否是控件数组的一部分。
      

  3.   

    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
      

  4.   

    只能在设计时创建,在代码中添加(Load)或删除(Unload)
      

  5.   

    load optButton(MaxIdx) '
      optButton(MaxIdx).top = optButton(MaxIdx - 1).top + 360
      optButton(MaxIdx).visible = true