source

사용자 양식에서 동적으로 추가된 컨트롤 제거

ittop 2023. 8. 30. 22:07
반응형

사용자 양식에서 동적으로 추가된 컨트롤 제거

확인란이 동적으로 추가된 Excel 사용자 양식이 있습니다.

다음과 같은 코드로 확인란을 추가합니다.

Set chkBox = Me.Controls.Add("Forms.Checkbox.1", "Checkbox" & i)

이 확인란을 모두 제거합니다.

Dim j As Integer
'Remove all dynamically updated checkboxes
For Each cont In Me.Controls
    For j = 1 To NumControls
        If cont.Name = "Checkbox" & j Then
            Me.Controls.Remove ("Checkbox" & j)
        End If
    Next j
Next cont

다음 오류 메시지가 표시됩니다.
Error MEssage

더 나은 방법은 사용자가 만든 컨트롤(예: 컬렉션)을 추적하여 해당 컨트롤을 제거하는 것입니다.

이렇게 하면 코드가 이름 형식에 바인딩되지 않으며 다른 제어 유형에도 적용될 수 있습니다.

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

"Checkbox"로 시작하는 다른 제어 이름이 없다고 가정하면,

For Each cont In Me.Controls
    If InStr(cont.Name, "Checkbox") = 1 Then
        Me.Controls.Remove cont.Name
    End If
Next cont

만약 당신이 이미 컨트롤의 이름, 타입, 그리고 몇 개인지 알고 있다면, 왜 이중 루프입니까?

런타임에 생성된 컨트롤만 제거할 수 있습니다.

'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

그리고 당신의 경우: '모든 명명이 맞다면.

Dim j&
For j = 1 To NumControls
    Me.Controls.Remove "Checkbox" & j
Next j

그리고 당신이 시도할 수 있는 또 다른 접근법.

Me.controls.clear ' from https://learn.microsoft.com/en-us/office/vba/Language/Reference/User-Interface-Help/clear-method-microsoft-forms

컨트롤에 대한 검사를 추가하면 이 문제가 해결되는 것처럼 보였습니다.왜 그런지는 모르겠지만, 효과가 있습니다.

   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

명령 버튼을 사용하여 원래 코드를 다시 작성하고 "Me.Controls"를 추가했습니다."NumControls"가 아닌 "Count"를 선택하고 "Cont"를 Control로 정의했습니다.그것은 저에게 효과가 있는 것 같습니다.이것이 당신에게 효과가 있는지 알려주시기 바랍니다.

-->

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

유지 및 삭제할 컨트롤을 선택하는 또 다른 방법은 를 사용하는 것입니다.태그 속성.이렇게 하면 예를 들어 를 사용하여 컨트롤을 추가할 때 일부 세부적으로 제어할 수 있습니다.비트 필드로 태그를 지정합니다.

작성 시:

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

정리할 때가 되면,

For Each C In Me.Controls
    If C.Tag = 1 Then Me.Controls.Remove C.Name
Next

언급URL : https://stackoverflow.com/questions/27790320/remove-dynamically-added-controls-from-userform

반응형