例如:
Set btnObj = Controls.Add("VB.CommandButton", "btnObj")
     With btnObj
    ' .Index = 1
     .Visible = True
     .Width = 2000
     .Caption = "Hello"
     .Top = 1000
     .Left = 1000
     End With
动态添加一个CommandButton控件以后,需要:
Private Sub btnObj_Click()
     MsgBox "这是动态添加的按钮。"
     MsgBox "remove self"
     Controls.Remove btnObj
    Set NewButton = Nothing
End Sub
这个类似的事件(生成控件以后再生效的)。不能用:
Private WithEvents btnObj as CommandButton
因为事先并不知道要动态生成多少个控件!
给个思路也行~~~~~~~~```

解决方案 »

  1.   

    如果可以确定,那就很简单了
    那表示你需要把一个事件的处理动态附加到控件上一个脏盆儿:假设你要动态添加一个按钮首先定义事件处理对象'类模块
    'Begin ButtonEventHandler
    Option ExplicitPrivate WithEvents mButton As CommandButtonPublic Sub Hook(ByVal CommandButton As Object)
        Set mButton = CommandButton
    End SubPrivate Sub mButton_Click()
        MsgBox "Add Your Code Here"
    End Sub
    'End ButtonEventHandler然后你就可以为动态添加的控件指定事件处理函数了'窗体模块private sub AddButton()
        Dim btnObj as CommandButton
        Dim EventHandler as new ButtonEventHandler
        Set btnObj = Controls.Add("VB.CommandButton", "btnObj")
         With btnObj
        ' .Index = 1
         .Visible = True
         .Width = 2000
         .Caption = "Hello"
         .Top = 1000
         .Left = 1000
         End With
        EventHandler.Hook btnObj '从这时开始,动态添加的控件的事件,就由ButtonEventHandler对象来处理了
    end sub引申一步,你还可以用这种方法为所有的控件动态的添加事件处理函数,也可以为一个控件指定多个事件处理函数
      

  2.   

    为什么不用 CommandButton.LOAD(I)
    可以方便一点啊!