现加一个commandbutton,将其index设为0,然后在程序中load(command1.ubound+1)

解决方案 »

  1.   

    同意上面的回复,但要将其显示出来的话需要设置一下其Visiable属性。
      

  2.   

    Private Sub Form_Load()
        Dim a As Object
        Set a = Me.Controls.Add("VB.CommandButton", "cmd")
        a.Visible = True
    End Sub
      

  3.   

    MSDN SAMPLESD:\Program Files\Microsoft Visual Studio\MSDN98\98VS\2052\SAMPLES\VB98\CtlsAdd
    CtlCfg.vbp 和 CtlView.vbp此文件夹中的两个工程文件演示了动态控件添加 (Form1.Controls.Add), 或者在运行时添加未引用控件到窗体, ActiveX 文档, 和用户控件。CtlCfg (控件配置) 示例允许您添加有关任何控件--无论是原有的或用户控件的信息--到 Controls.mdb 数据库。 更重要的是, 如果加载或添加控件需要许可关键字,此数据库正是存储这个控件的许可关键字。这个数据库也可以存储用于控件被动态添加时设置的变量属性。  一旦有关控件的信息被加入数据库,您可以打开 ctlView (控件浏览)示例。这个示例简单地读取
    并显示您所能动态添加的控件列表。您可以通过在列表中单击来添加任意控件到此应用程序。 注意: 确定文件 Controls.mdb 具有可写属性。如果想了解更多有关动态控件添加的信息,请参阅下列帮助主题:Add Method (Controls Collection)
    Add Method (Licenses Collection)
    EventInfo Object
    Licenses Collection
    ObjectEvent Event
    Parameter Object (Visual Basic)
      

  4.   

    Dim WithEvents cmdMyCommand As VB.CommandButton
        Option Explicit    Dim WithEvents ctlDynamic As VBControlExtender
        Dim WithEvents ctlText As VB.TextBox
        Dim WithEvents ctlCommand As VB.CommandButton
        Dim WithEvents ctlCommandDel As VB.CommandButton
        Private Sub ctlCommandDel_Click()
            Dim i As Integer
            
            Licenses.Remove "MSComctlLib.TreeCtrl"
            If MsgBox("是否删除所有控件", vbYesNo) = vbYes Then
            For i = 1 To Form1.Controls.Count
                Controls.Remove 0
            Next i
            End If
        End Sub
        Private Sub ctlCommand_Click()
            ctlText.Text = "你点击的是控制按钮"
        End Sub    Private Sub ctlDynamic_ObjectEvent(Info As EventInfo)
            If Info.Name = "Click" Then
                ctlText.Text = "你点击的条目是 " & _
                        ctlDynamic.object.selecteditem.Text
            End If
        End Sub    Private Sub Form_Load()
            Dim i As Integer
            Licenses.Add "MSComctlLib.TreeCtrl"        Set ctlDynamic = Controls.Add("MSComctlLib.TreeCtrl", _
                            "myctl", Form1)
            ctlDynamic.Move 1, 1, 2500, 3500
            For i = 1 To 10
                ctlDynamic.object.nodes.Add Key:="Test" & Str(i), _
                        Text:="Test" & Str(i)
                ctlDynamic.object.nodes.Add Relative:="Test" & Str(i), _
                        Relationship:=4, Text:="TestChild" & Str(i)
            Next i
            ctlDynamic.Visible = True        Set ctlText = Controls.Add("VB.TextBox", "ctlText1", Form1)
            ctlText.Move (ctlDynamic.Left + ctlDynamic.Width + 50), _
                            1, 2500, 100
            ctlText.BackColor = vbBlue
            ctlText.ForeColor = vbWhite
            ctlText.Visible = True        Set ctlCommand = Controls.Add("VB.CommandButton", _
                            "ctlCommand1", Form1)
            ctlCommand.Move (ctlDynamic.Left + ctlDynamic.Width + 50), _
                            ctlText.Height + 50, 1500, 500
            ctlCommand.Caption = "点击"
            ctlCommand.Visible = True
            
            Set ctlCommandDel = Controls.Add("VB.CommandButton", _
                            "ctlCommand2", Form1)
            ctlCommandDel.Move (ctlDynamic.Left + ctlDynamic.Width + 50), _
                            ctlText.Height + 650, 1500, 500
            ctlCommandDel.Caption = "删除所有控件"
            ctlCommandDel.Visible = True
        End Sub
        
    参考 http://www.applevb.com/art/dyn_control.txt