Results 1 to 3 of 3

Thread: Dynamic Control Collection - unable to get/set control properties

  1. #1
    New Member
    Join Date
    Oct 12
    Posts
    5

    Exclamation 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

  2. #2
    PowerPoster
    Join Date
    Dec 04
    Posts
    18,592

    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
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    Fanatic Member
    Join Date
    Sep 12
    Location
    To the moon and then left
    Posts
    579

    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
    Last edited by Zvoni; Oct 29th, 2012 at 03:33 AM.
    For health reasons i try to avoid reading unformatted Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •