我用
Add方法在Controls集合中添加多个控件
set newbutton=Object.Add(ProgID,Name,Container)
name各不相同。这里以name1,name2,name3,name4说明。
想用Object.Remove newbutton来删除控件
这样只能删除做后一个控件,怎样用set newbutton来选中前几个控件呀。

解决方案 »

  1.   

    如果你加的控件是同一种控件的话,我建议你用控件数组,需要的时候load一个,不需要的时候unload,通过数组的id可以控制unload前面的控件。
      

  2.   

    Option Explicit
      '  ′通过使用WithEvents关键字声明一个对象变量为新的命令按钮
        Private WithEvents NewButton As CommandButton
            Private WithEvents NewButton1 As CommandButton   ' ′增加控件
        Private Sub Command1_Click()
        If NewButton Is Nothing Then
       ' ′增加新的按钮cmdNew
        Set NewButton = Controls.Add("VB.CommandButton", "cmdNew", Me)
       ' ′确定新增按钮cmdNew的位置
        NewButton.Move Command1.Left + Command1.Width + 240, Command1.Top
        NewButton.Caption = "新增的按钮"
        NewButton.Visible = True    End If
         If NewButton1 Is Nothing Then
                Set NewButton1 = Controls.Add("VB.CommandButton", "cmdold", Me)
            NewButton1.Move Command1.Left + Command1.Width + 1840, Command1.Top
        NewButton1.Caption = "新增按钮"
        NewButton1.Visible = True
    End If    End Sub
      ' ′删除控件(注:只能删除动态增加的控件)
        Private Sub Command2_Click()
        If NewButton Is Nothing Then
        Else
        Controls.Remove NewButton
        Set NewButton = Nothing
        End If
        End Sub
      
      

  3.   

    Option Explicit
        '通过使用WithEvents关键字声明一个对象变量为新的命令按钮
    Private WithEvents NewButton As CommandButton
     '增加控件
    dim i '控件数量
    Private Sub Command1_Click()        '增加新的按钮cmdNew        Set NewButton = Controls.Add("VB.CommandButton", "cmdNew"&i, Me)        '确定新增按钮cmdNew的位置        NewButton.Move Command1.Left + Command1.Width + 240*i, Command1.Top        NewButton.Caption = "新增的按钮"        NewButton.Visible = True        End If        End Sub        '新增控件的单击事件Private Sub Command2_Click()'删除控件    'Set NewButton = Controls.Item(0)'怎样才能删除第一个控件
        Controls.Remove NewButton
        Set NewButton = NothingEnd Sub
      

  4.   

    '对不起,搞错了
    Option Explicit
      '  ′通过使用WithEvents关键字声明一个对象变量为新的命令按钮
        Private WithEvents NewButton As CommandButton
            Private WithEvents NewButton1 As CommandButton   ' ′增加控件
        Private Sub Command1_Click()
        If NewButton Is Nothing Then
       ' ′增加新的按钮cmdNew
        Set NewButton = Controls.Add("VB.CommandButton", "cmdNew", Me)
       ' ′确定新增按钮cmdNew的位置
        NewButton.Move Command1.Left + Command1.Width + 240, Command1.Top
        NewButton.Caption = "新增的按钮"
        NewButton.Visible = True    End If
         If NewButton1 Is Nothing Then
                Set NewButton1 = Controls.Add("VB.CommandButton", "cmdold", Me)
            NewButton1.Move Command1.Left + Command1.Width + 1840, Command1.Top
        NewButton1.Caption = "后增按钮"
        NewButton1.Visible = True
    End If    End Sub
      ' ′删除控件(注:只能删除动态增加的控件)
        Private Sub Command2_Click()
        If NewButton Is Nothing Then
        Else
        Controls.Remove NewButton
        Set NewButton = Nothing
        End If
        End Sub
        '′新增控件的单击事件
        Private Sub NewButton_Click()
        MsgBox "您选中的是动态增加的按钮!"
        End Sub
      
      

  5.   

    如yu1928(小鱼)的:unload "控件名"
      

  6.   

    dim e as Variant
    for each e in form1.controls
        if typeof e is textbox then controls.remove e
        if left(e.name)="RuntimeCtl" then controls.remove e
    next试试吧.代码我没测试
      

  7.   

    问题的关键是我不知道怎样获得第一次动态增加的按钮
    set newbutton=???
    若能获得就可以利用
    Controls.Remove NewButton
    删除控件!