title: Creating an userform object hierarchy with grouping in vba
body: <p>First things first. There's a good chance what I want to do should really be done with VB and not VBA. But as long as it is possible I would rather use VBA.</p>
  
  <p>I have a userform of essentially a big diagram made of hundreds of labels. I want to separate these labels into groups. And then separate these groups into subsystems. The idea being I have some form of heirarchy to work with. The label groups need to change color based on what I have selected in a combo box, and if I click on one of these labels I want to bring up a user form showing details of the subsystem using click events.</p>
  
  <p>I'm pretty sure I need to use multiple classes to do what I want but am fairly new to using class modules. Though I get the concept. </p>
  
  <p>Basically I want some functionality that goes subsystem -> label group( or part) -> color with click events for the whole subsystem and combo box events for changing label group colors.
  I saw a thread online about grouping labels or text boxes but it only works to trigger the even for a group, not change the properties of the whole group once the event is triggered. I would like to set this up in classes as well so I can export the system for use in other future userforms.</p>
  
  <p>I was able to create groups of labels and change them together like I wanted:</p>
  
  <p>CPart (Class Module 1):
  *This is meant to handle the event triggering of the labels and includes some color code that I used to test functionality of the groups changing together and functionality of changing colors.
  Public WithEvents trigger As MSForms.Label</p>
  
  <pre><code>Dim pLabels As Collection
  
  Property Set triggers(c As Collection)
      Set pLabels = c
  End Property
  
  Private Sub trigger_Click()
      For Each obj In pLabels
          obj.BackColor = RGB(255, 0, 0)
      Next obj
  End Sub
  </code></pre>
  
  <p>CTrigger (Class Module 2):
  *This took a collection of labels which were passed in through a collection variable in the userform and then stored each label as a trigger in a separate class variable, along with the whole collection of labels in that group. This way when any trigger fires the event, all of the labels change.</p>
  
  <pre><code>Dim CTrigger() As New CPart
  Dim pLabels As Collection
  Dim i As Integer
  
  Property Set Labels(c As Collection)
      Set pLabels = c
      For i = 1 To pLabels.Count
          ReDim Preserve CTrigger(1 To i)
          Set CTrigger(i).trigger = pLabels.Item(i)
          Set CTrigger(i).triggers = pLabels
      Next i
  End Property
  
  Property Get Labels() As Collection
      Labels = pLabels
  End Property
  </code></pre>
  
  <p>I really don't like the way it works, partly because I am losing myself in the logic of it constantly, and partly because it means that in order to use this I have to make collections of labels in the userform module anyway just to run it. It is very inefficient code, but I am putting it up so you get an idea of what I am trying to accomplish.</p>
  
  <p>What I would much rather do instead is have one class variable to hold my custom collection of labels (a "LabelGroup"). Another class variable is likely required to hold the labels themselves (I think). And then all I would have to do is go through and write methods for the LabelGroup class such as changecolor, and it could handle that. But I can handle that part, for now what I really need help with is setting up the class framework in a neat way, so that the module I will eventually run could just say things like:</p>
  
  <pre><code>LabelGroup1.Add Label1
  LabelGroup2.Add Label2
  </code></pre>
  
  <p>or</p>
  
  <pre><code>Private Sub button_click()
      LabelGroup1.ChangeColor(RGB(...))
  End Sub
  </code></pre>
  
  <p>These two articles have been helping me along:
  <a href="http://www.databaseadvisors.com/newsletters/newsletter200503/0503usingcustomcollections/using%20custom%20collections%20in%20microsoft%20access.asp" rel="nofollow">http://www.databaseadvisors.com/newsletters/newsletter200503/0503usingcustomcollections/using%20custom%20collections%20in%20microsoft%20access.asp</a></p>
  
  <p><a href="http://j-walk.com/ss/excel/tips/tip44.htm" rel="nofollow">http://j-walk.com/ss/excel/tips/tip44.htm</a></p>