Private Sub Form_Load()
    For i = 0 To 10
        temp = "LB" & i
        If i <> 0 Then
            Set Lab(i) = Me.Controls.Add("VB.Label", temp, Me)
        Else
            Set Lab(0) = Label1(0)
        End If
        Lab(i).move 0,Lab(0).Height,Lab(0).Width,Lab(0).Height * i
        Lab(i).Visible = True
        Lab(i).Alignment = 2
        Lab(i).BackStyle = 0
        Lab(i).Caption = i
    Next
End Sub
为什么这样生成的控件类,不能触发事件,高手指点
谢谢

解决方案 »

  1.   

    Option ExplicitPrivate WithEvents cmdButton As CommandButtonPublic Function ObjectAdd( _
        ByVal ObjParent As Object, ByVal ObjType As String, ByVal OBJName As String _
    ) As Object
        On Error Resume Next
        Set ObjectAdd = ObjParent.Controls.Add(ObjType, OBJName)    Call Err.Clear
        DoEvents
    End FunctionPrivate Sub cmdButton_Click()
        MsgBox "Success!!"
    End SubPrivate Sub Command1_Click()
    Set cmdButton = ObjectAdd(Me, "Vb.CommandButton", "Button")
    With cmdButton
    .Visible = True '显示出来
    .Width = 1000
    .Height = 1000
    .Caption = "fuck u"
    .Top = 100
    .Left = 200
    End WithEnd Sub
      

  2.   

    楼上的方法的确不错,只是还有两点不是太好:
    1,我测试了,这样的方法好像只能定义一个控件,我想用多个,用控件数组怎么办.
    2,这样只能在触发 Command1_Click()事件后,才能用,能不能在窗口载入时就让它出现希望再给点建意谢谢
      

  3.   

    我以前也用这个方法试过,不好用。后来用控件数组load就可以了。
    首先建立一个控件,把它的Index设置成0,后面再用load命令动态增加,卸载时用unload函数。