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
 
Thanks Domenic, for showing me how to iterate the collection; unfortunately iterating the collection doesn't seem to find the information about the control that I seek; still would like to find member name 'Lableza' some how.
I kid you not; there is no code for the class as of yet; I'm trying to discover a procedure for identifying the class members in a loop, by there class member name. I'm not hiding anything here.
Here is the class declaration and private sub.
Code:
Public WithEvents labelzA As MSForms.Label
Private Sub labelzA_Click()
    Dim cn As Object
    
    For Each cn In UserForm1.Controls
        If "cn class member name" = labelzA Then
        
        End If
    Next cn
End Sub
 
Upvote 0

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
I'm still confused as to what precisely your problem is. Perhaps time for a different take on this: What is it that you are trying to achieve?
 
Upvote 0
I've created a subroutine that creates many, many Label controls on a User form. From my first example in this thread, where I create the labels, you can see I'm assigning them to a class event (Either Platformcontrols.LabelzA or Platformcontrols.LabelzB.... I did forget to assign LabelzB in that example as someone later pointed out.)
So; for example, if I have 100 labels of Platformcontrols.LabelzA and 100 labels of Platformcontrols.LabelzB, I was looking for the ability to loop though these userform controls and be able to identify which ones are from LabelzA.
The reason is because I was experimenting with re-purposing a control during runtime; Convert a LablesA to a LablesB; I'm able to accomplish this but would be nice if I could re-purpose many controls in a loop that were LabelzA, to whatever I want.
Why would I want to repurpose a control? Because I have built some templates that build out hundreds of controls for the purpose of carrying out search tasks on some data and would rather reassign different functionality to some controls when moving to alternate data sets without actually having to copy all the code to another module where I'd then change just the LabelzA to whatever.
In short, a giant piece of code can be used for different programs by switching some functionality on the fly of certain controls.
I think you guys understand how LabelzA and LabelzB are from my Class PlatformControls; So I don't understand when you say how you don't understand the question when all I'm wanting to do is loop though all controls in the form, and be able to identify those who are from LabelzA. I'm being quite clear. No one has posted a solution that allows me to do such a task; you just keep saying you don't understand. I don't think this is case of understanding; it's a case of either it can't be done or you don't know how.


Here is repurposing LabelzA control to a LabelzB
Code:
Private Sub labelzA_Click()
    MEM1.Remove labelzA.Name
    With labelzA
        Set C1 = New PlatformControls
        Set C1.labelzB = LAB
        MEM1.Add Item:=C1, Key:=labelzA.Name
    End With
End Sub
Private Sub labelzB_Click()
    MsgBox "success"
End Sub

Here is what I'd like to do from subroutine in my project:
Code:
    For Each CNTRL In UserForm1.Controls
        If "control class member name is = Labelza" Then
        
              'and then reassing to labelzb
        End If
    Next CNTRL
 
Last edited:
Upvote 0
Assuming MEM1 is a collection in the userform's code I would:
- Upgrade MEM1 to be a class
- add two collections to the new class to hold labelza's and labelzb's
- Add logic to class to add or remove controls from either collection
- Add logic to the class to return a true or false whether a control is in the labelza or labelzb collection
- Add lgic to move a control from a to b or vice versa
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

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