Add 方法(Controls 集合)
      在 Controls 集合中添加一个控件并返回一个对该控件的引用。语法object.Add (ProgID, name, container)Add 方法(Controls 集合)示例
Private Sub Form_Load()
   Form1.Controls.Add "VB.CommandButton", "cmdObj1", Frame1
   With Form1!cmdObj1
      .Visible = True
      .Width = 2000
      .Caption = "Dynamic Button"
   End With
End Sub注意 上面的代码例子使用 ! 作为一个语法要素。您也可以使用标准集合语法如 Form1.Controls("cmdObj1") 来引用该控件。第二个例子使用 WithEvents 关键字声明一个 CommandButton 类型的对象变量,允许您编程该控件的事件。对象变量被设置为由 Add 方法返回的引用。要试验该例,把它的代码粘贴到声明部分并且运行该工程。Option Explicit
Private WithEvents btnObj As CommandButtonPrivate Sub btnObj_Click()
   MsgBox "This is a dynamically added button."
End SubPrivate Sub Form_Load()
   Set btnObj = Controls.Add("VB.CommandButton", "btnObj")
   With btnObj
      .Visible = True
      .Width = 2000
      .Caption = "Hello"
      .Top = 1000
      .Left = 1000
   End With
End Sub第三个例子添加一个非引用控件到 Controls 集合。然而要编程这样一个控件的事件,必须声明一个 VBControlExtender 类型的对象变量,并把由这个方法返回的引用设置到该对象。然后使用 ObjectEvent 事件编程该控件的事件。Option Explicit
Dim ctlExtender As VBControlExtenderPrivate Sub Form_Load()
   Set ctlExtender = Controls.Add("Project1.UserControl1", "MyControl")
   With ctlExtender 
      .Visible = True
      .Top = 1000
      .Left = 1000
   End With
End SubPrivate Sub extObj_ObjectEvent(Info As EventInfo)
   '使用 Select Case 编程该控件事件。
   Select Case Info.Name
   Case "UserName"
      '检查用户名值。
      MsgBox Info. EventParameters("UserName").Value
   '现在显示其他情况
   Case Else '未知事件
      '这里处理未知事件。
   End Select
End Sub

解决方案 »

  1.   

    load方法:1.把command1.index 改为02.load command1(i) 'i为不为0的整数
      

  2.   

    dim a as new cammand1  错误。
    不能这样添加控件。
      

  3.   

    dim ctr as control
    Set ctr = Controls.Add("VB.CommandButton", "command1")
    command1.visible=true
    .....................
      

  4.   

    Private Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" (ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, lpParam As Any) As Long
    Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As LongConst WS_CHILD = &H40000000
    Const SW_NORMAL = 1
    Private Sub Command1_Click()
    Dim ncWnd As Long
        ncWnd = CreateWindowEx(ByVal 0&, "BUTTON", "Hello !", WS_CHILD, 0, 0, 100, 100, Me.hwnd, ByVal 0&, App.hInstance, ByVal 0&)ShowWindow ncWnd, SW_NORMAL
    End Sub