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:
CLSLabels 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
CLSLinkEvents 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
I want to use this code to capture the click events of dynamically created labels.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 have this in my declarations:
I define a Label collection at startup:Code:Private WithEvents Lbl As MSForms.Label Private WithEvents Labels As ClsLabels
I create the dynamic controls:Code:Private Sub UserForm_Initialize() Set Labels = New ClsLabels End Sub
And I have an eventhandler that is correctly being triggered: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
But I can't seem to be able to set the label properties, such as the caption.Code:Private Sub Labels_Click(ByVal ControlName As String, ByVal Index As Integer) MsgBox ControlName & " " & Index End Sub
For example:
I've been searching on this issue for hours. And I have a deadline to meet. Can someone please help??Code:Private Sub Labels_Click(ByVal ControlName As String, ByVal Index As Integer) Labels(Index).Label.Caption = "FAIL" End Sub
Thanks in advance for your clues.
Pieter


Reply With Quote
