Remove Dynamically Added Controls from Userform

前端 未结 6 422
野的像风
野的像风 2021-01-02 01:13

I have an Excel userform with dynamically added checkboxes.

I add the checkboxes with code that looks like this:

Set chkBox = Me.Controls.Add(\"Forms         


        
相关标签:
6条回答
  • 2021-01-02 01:49

    Adding a check for the control seemed to fix this. Not entirely sure why, but it works.

       Dim j As Integer
    'Remove all dynamically updated checkboxes
    For Each cont In Me.Controls
        If TypeName(cont) = "CheckBox" Then
            For j = 1 To NumControls
                If cont.Name = "Checkbox" & j Then
                    Me.Controls.Remove cont.Name
                    Exit For
                End If
            Next j
        End If
    Next cont
    
    0 讨论(0)
  • 2021-01-02 01:51

    Another way to select which controls are kept and which are deleted is to use the .Tag attribute. This allows some fine control of the controls as they are added, for example by using the .Tag as a bitfield.

    At creation time:

    With Me.Controls.add("Forms.Label.1", Visible:=True)
    {code}
        .Tag = 1
    {more code}
    

    Then when the time comes to tidy up:

    For Each C In Me.Controls
        If C.Tag = 1 Then Me.Controls.Remove C.Name
    Next
    
    0 讨论(0)
  • 2021-01-02 01:55

    A better approach may be to keep track of the controls you create (eg in a collection), and use that to remove them.

    This way your code is not bound to the name format, and can be applied to other control types too.

    Private cbxs As Collection
    
    Private Sub UserForm_Initialize()
        Set cbxs = New Collection
    End Sub
    
    ' Remove all dynamicly added Controls
    Private Sub btnRemove_Click()
        Dim i As Long
        Do While cbxs.Count > 0
            Me.Controls.Remove cbxs.Item(1).Name
            cbxs.Remove 1
        Loop
    End Sub
    
    
    ' Add some Controls, example for testing purposes
    Private Sub btnAdd_Click()
        Dim i As Long
        Dim chkBox As Control
        For i = 1 To 10
            Set chkBox = Me.Controls.Add("Forms.CheckBox.1", "SomeRandomName" & i)
            chkBox.Top = 40 + i * 20
            chkBox.Left = 20
            cbxs.Add chkBox, chkBox.Name  '- populate tracking collection
        Next
    
        ' Demo that it works for other control types
        For i = 1 To 10
            Set chkBox = Me.Controls.Add("Forms.ListBox.1", "SomeOtherRandomName" & i)
            chkBox.Top = 40 + i * 20
            chkBox.Left = 60
            cbxs.Add chkBox, chkBox.Name
        Next
    
    End Sub
    
    0 讨论(0)
  • 2021-01-02 01:56

    Assuming there are no othe control names starting with "Checkbox",

    For Each cont In Me.Controls
        If InStr(cont.Name, "Checkbox") = 1 Then
            Me.Controls.Remove cont.Name
        End If
    Next cont
    
    0 讨论(0)
  • 2021-01-02 02:03

    if you already know the name of the controls, the type, and how many, why double loop ?

    note that ONLY controls created at runtime can be removed.

    'the following removes all controls created at runtime
    Dim i As Long
    On Error Resume Next
    With Me.Controls
        For i = .Count - 1 to 0 step -1
            .Remove i
        Next i
    End With
    Err.Clear: On Error GoTo 0
    

    and for your case : 'if all naming are correct

    Dim j&
    For j = 1 To NumControls
        Me.Controls.Remove "Checkbox" & j
    Next j
    
    0 讨论(0)
  • 2021-01-02 02:04

    I rewrote the original code using command buttons, and just added "Me.Controls.Count" rather than "NumControls" and defined "Cont" as a Control. It seems to be working for me. Please let me know if this works for you:

    -->

    On Error Resume Next
    Dim Cont As Control
    Dim C As Integer
    'Remove all dynamically updated checkboxes
    For Each Cont In Me.Controls
        For C = 1 To Me.Controls.Count
        If Cont.Name = "CommandButton" & C Then
            Me.Controls.Remove ("CommandButton" & C)
        End If
        Next C
    Next Cont
    
    0 讨论(0)
提交回复
热议问题