Dim WithEvents btnobj As CommandButtonPrivate Sub btnobj_Click()
    MsgBox "ok"
End SubPrivate Sub Command1_Click()
Static i As Integer
Dim capt As String
Dim leftx As Integer
Dim topx As Integer
leftx = 1000
topx = 1000
   Do
     i = i + 1
     capt = "Command" & i
     'leftx = leftx + i * 500
    ' topx = topx + i * 500
   Exit Do
   Loop
   capt = "cmd"
   'Text1.Text = i
Set btnobj = Controls.Add("VB.CommandButton", capt)
  With btnobj
     '.Index = 0
     .Visible = True
     .Width = 2000
     .Caption = capt
     .Top = 1000 ' topx
     .Left = 1000 ' leftx
     
     .ZOrder
     '.SetFocus
  End With
End Sub我去掉了您的
.DragMode = 1
不然引发不了事件

解决方案 »

  1.   

    可以用WithEvents设定控件的事件
    像上面的btnobject要响应click事件的话,可以添加
    priavte WithEvents btnobject As CommandButton然后就可以使用btnobject_click()事件了,当然你必须自己添加这些代码。
      

  2.   

    是的,还没有。还有其他的办法吗?.DragMode = 1是不能去掉的,因为如果去掉了,控件就不能手动拖动了。
      

  3.   

    Form1中
    Private Sub Command3_Click()
        Dim frm As New Form2
        Form2.AutoAddControl
        Form2.Show
    End SubForm2中
    Dim WithEvents btnobj As CommandButtonPrivate Sub btnobj_Click()
        MsgBox "ok"
    End SubSub AutoAddControl()
        Set btnobj = Controls.Add("VB.CommandButton", "cmd")
        With btnobj
         .Visible = True
         .Width = 2000
         .Caption = "cmd"
         .Top = 1000 ' topx
         .Left = 1000 ' leftx
        End With
    End Sub
    另外DragMode请在您需要的时候再设置为1,不然是引发不了事件的