Dynamic Control Collection - unable to get/set control properties
Hi guys,
I think I have a lack of sleep cause I can't seem to figure this out.
I have 3 classes:
CLSLabel Class:
Code:
Option Explicit
Private WithEvents m_Label As MSForms.Label
Private m_LinkEvents As ClsLinkEvents
Private m_Index As Integer
Public Property Set LinkEvents(ByRef LinkEvents As ClsLinkEvents)
Set m_LinkEvents = LinkEvents
End Property
Public Property Get Index() As Integer
Index = m_Index
End Property
Public Property Let Index(ByVal idx As Integer)
m_Index = idx
End Property
Public Property Get Label() As MSForms.Label
Set Label = m_Label
End Property
Public Property Set Label(ByVal Lbl As MSForms.Label)
Set m_Label = Lbl
End Property
Private Sub m_Label_Click()
m_LinkEvents.FireClick m_Label.Name, Index
End Sub
CLSLabels Class:
Code:
Option Explicit
Public Event Click(ByVal ControlName As String, ByVal Index As Integer)
Private WithEvents m_LinkEvents As ClsLinkEvents
Private m_Collect As Collection
Private Sub Class_Initialize()
Set m_Collect = New Collection
Set m_LinkEvents = New ClsLinkEvents
End Sub
Private Sub m_LinkEvents_Click(ByVal ControlName As String, ByVal Index As Integer)
RaiseEvent Click(ControlName, Index)
End Sub
Public Sub AddItem(ByRef Lbl As MSForms.Label, Optional ByVal Key As Variant)
Dim xLbl As ClsLabel
Set xLbl = New ClsLabel
Set xLbl.Label = Lbl
Set xLbl.LinkEvents = m_LinkEvents
If IsMissing(Key) Then
m_Collect.Add xLbl
Else
m_Collect.Add xLbl, Key
End If
m_Collect(m_Collect.Count).Index = m_Collect.Count
End Sub
Public Sub RemoveItem(ByVal Index As Integer)
m_Collect.Remove Index
End Sub
Public Function Count() As Long
Count = m_Collect.Count
End Function
Public Property Get Item(ByVal Index As Variant) As ClsLabel
Set Item = m_Collect(Index)
End Property
CLSLinkEvents Class:
Code:
Option Explicit
Public Event Click(ByVal ControlName As String, ByVal Index As Integer)
Public Sub FireClick(ByVal ControlName As String, ByVal Index As Integer)
RaiseEvent Click(ControlName, Index)
End Sub
I want to use this code to capture the click events of dynamically created labels.
I have this in my declarations:
Code:
Private WithEvents Lbl As MSForms.Label
Private WithEvents Labels As ClsLabels
I define a Label collection at startup:
Code:
Private Sub UserForm_Initialize()
Set Labels = New ClsLabels
End Sub
I create the dynamic controls:
Code:
Set Lbl = Me.Controls.Add("Forms.label.1", "Labelname", True)
With Lbl
.Height = 100
.Width = 100
.Top = 0
.Left = 0
End With
Labels.AddItem Lbl
And I have an eventhandler that is correctly being triggered:
Code:
Private Sub Labels_Click(ByVal ControlName As String, ByVal Index As Integer)
MsgBox ControlName & " " & Index
End Sub
But I can't seem to be able to set the label properties, such as the caption.
For example:
Code:
Private Sub Labels_Click(ByVal ControlName As String, ByVal Index As Integer)
Labels(Index).Label.Caption = "FAIL"
End Sub
I've been searching on this issue for hours. And I have a deadline to meet. Can someone please help??
Thanks in advance for your clues.
Pieter
Re: Dynamic Control Collection - unable to get/set control properties
i think you have over complexed it, into a recursive firing of event
Code:
Private Sub m_Label_Click()
m_Label.Caption = InputBox("enter new caption")
End Sub
Re: Dynamic Control Collection - unable to get/set control properties
You're passing the name of your label instead of the label itself in your click-event. When you Click on your label you have to first Set the reference to the clicked label.
EDIT: For some reason this line is bugging me.
Code:
Private Sub Labels_Click(ByVal ControlName As String, ByVal Index As Integer)
Labels(Index).Label.Caption = "FAIL"
End Sub
Shouldn't that be:
Code:
Private Sub Labels_Click(ByVal ControlName As String, ByVal Index As Integer)
Labels.Item(Index).Caption = "FAIL"
End Sub