iterate many controls by class event name

RAYLWARD102

Well-known Member
Joined
May 27, 2010
Messages
529
Can it be done?
Let say I create 200 labels on a user form; 100 of which are C1.labelzA and 100 of which are C1.labelzB

Code:
Private Sub UserForm_Initialize()
    Dim x As Long
    Set MEM1 = New Collection
    For x = 1 To 100
        Set LAB = UserForm1.Controls.Add("Forms.Label.1")
        With LAB
            .Caption = ""
            .Height = 100
            .Width = 500
            .Left = 0
            .Top = 0
            .SpecialEffect = fmSpecialEffectFlat
            .Name = "labelA" & x
            .Font.size = .Height * 0.5
            .BackStyle = fmBackStyleOpaque
            .BackColor = vbWhite
            .ForeColor = RGB(192, 192, 192)
            .TextAlign = fmTextAlignCenter
            Set C1 = New PlatformControls
            Set C1.labelzA = LAB
            MEM1.Add Item:=C1, Key:=.Name
        End With
        
        Set LAB = UserForm1.Controls.Add("Forms.Label.1")
        With LAB
            .Caption = ""
            .Height = 100
            .Width = 500
            .Left = 0
            .Top = 0
            .SpecialEffect = fmSpecialEffectFlat
            .Name = "labelB" & x
            .Font.size = .Height * 0.5
            .BackStyle = fmBackStyleOpaque
            .BackColor = vbWhite
            .ForeColor = RGB(192, 192, 192)
            .TextAlign = fmTextAlignCenter
            Set C1 = New PlatformControls
            Set C1.labelzA = LAB
            MEM1.Add Item:=C1, Key:=.Name
        End With
    Next x
End Sub

Later on, would klike to iterate through controls and find controls from c1.labelzA; who to do that without using names???

Code:
    For Each CNTRL In UserForm1.Controls
        If "control event name is labelza" Then
        
        End If
    Next CNTRL
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
It looks like for each iteration you're assigning both labels to labelzA. I'm assuming that the second one should be assigned to labelzB, correct? Also, I'm assuming that you've declared your collection MEM1 at the module level, correct? If so, you can iterate through your controls as follows...

Code:
Private Sub CommandButton1_Click()
    Dim C1 As PlatformControls
    Dim oLabel As MSForms.Label
    For Each C1 In MEM1
        Set oLabel = C1.labelzA
        If Not oLabel Is Nothing Then
            Debug.Print oLabel.Name
            Set oLabel = Nothing
        End If
    Next C1
End Sub

Hope this helps!
 
Last edited:
Upvote 0
Correct; i forgot to assign labelzb
Still woul like to loop through labelza controls without using .name or .tag properties. There’s gotta be away to identify them by class member name labelza in a loop (Would make life so much simpler when updating thousands of controls dynamically. Currently I use a string array to store the names (not in example), but searching for the cleaner way
 
Upvote 0
So this means that for any one class object you will ever only have either labelzA or labelzB assigned a label, but not both, correct? If so, why not have only one property for any one object, let's say labelz. Then have two collections, instead of one. One for labelzA and the other for labelzB. Then you could simply iterate through the desired collection. Wouldn't this work?
 
Upvote 0
I have at least 10 classes with hundreds of members (labelza, labelzb....)
I simplified the example to make my point.
I’ve never able to iterate a collection; and collection only really contains the name of the control. I believe the collection knows what named control belongs to which class member, but have not like I said, successfully iterated a collection; all examples online show people adding some kind of dictionary reference or something and I get lost.
The property angle you speak of; is there a way to add properties to a control? Tge reason why I can’t use tag, is because It’s already being used dynamically, for other purposes that are dynamically changing; same with names
 
Upvote 0
I’ve never able to iterate a collection; and collection only really contains the name of the control.

Actually, in this case, the collection contains your class objects, from which you can access their properties and methods.

The property angle you speak of; is there a way to add properties to a control?

No, but you can create additional properties or methods for you class.

Tge reason why I can’t use tag, is because It’s already being used dynamically, for other purposes that are dynamically changing

Maybe you can have two components that make up the tag name, with the components separated by a delimiter. So, for example, if the first component specifies labelzA or labelzB, and the second components specifies whatever else, using variables A and B for the first and second components respectively, you would assign text to the Tag property as follows...

Code:
oLabel.Tag = A & "|" & B

Then to get the first component, you would do it like this...

Code:
var = split(oLabel.Tag, "|")(0)

Would this work?
 
Upvote 0
Don't want to store the values in tag as a csv; I have many vba programs utilizing tag the same way (1 value); I've already tracked these controls via array previously, but feel there has to be a way in finding the class member name. I'm already able to derive class name, control type, and user form names from a control but need the class member name. People talk about iterating through collections, but have never successfully been able to do it myself; anyone have an idea how to rad the values in a loop, from a collection? There might be info in the collection that I could be using.
 
Upvote 0
Hi jkpieterse,

There is nothing to post; everything has been posted above. The question, is how to derive a controls class member name while looping through all controls contained in a user form. My example was to create 100 labels from class PlatFormControls.Labelza and 100 labels from PlatFormControls.Lablezb. From another control in the userform, I'd like to loop all of these controls and determine which controls belong to PlatFormControls.Labelza
something like:

Code:
    For Each CNTRL In UserForm1.Controls
        If "control class member name is = Labelza" Then
        
        End If
    Next CNTRL

I realize the above code wouldn't work; just trying to explain what I'm seeking
 
Last edited:
Upvote 0
Code:
[COLOR=#333333]People talk about iterating through collections, but have never successfully been able to do it myself; [/COLOR]

From your example, each newly created class object is added to the collection "MEM1" after being assigned a label. And so I've shown you in Post #2 how to iterate through the collection.

What's not clear is whether each class object is being assigned labelzA, labelzB, etc, or whether only one of these will ever be assigned to any one class object. It would help to clarify the situation. And, it would help to post the code that you're using for your class, as Jan has requested.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,331
Members
452,636
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top