How to use a Class with a Collection of Controls

后端 未结 2 1000
无人及你
无人及你 2021-01-26 17:55

I tried adapting the solution in the link below to make a collection of text boxes allow numbers only. I get no error but the class just doesn\'t apply to the textboxes.

<
2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-26 18:42

    You need to put the obj objects in the collection, not the controls themselves

    Untested:

    Dim tbCollection As Collection
    
    Private Sub UserForm_Initialize()
        Dim obj As clsTextBox
        Dim arr
        Dim ctrl
    
        Set tbCollection = New Collection
    
        arr = Array(Me.tbAC, Me.tbCR, Me.tbHP) '<< edit: no Set
    
        For Each ctrl in arr
            Set obj = New clsTextBox
            Set obj.Control = ctrl
            tbCollection.Add obj
        Next
    
    End Sub
    

提交回复
热议问题