Optimize dynamic creation of labels and their click events.

bradyboyy88

Well-known Member
Joined
Feb 25, 2015
Messages
562
So my code allows to create a label dynamically (CreateLabel Sub) and dynamically remove labels (RemoveControls Sub) and the class module allows for the click event to occur for these dynamically created labels. The thing is I am using both collection and arrays to make this happen but I feel as though there has to be a way to get this just to the collection object and possibly reduce my code and make more efficient then having to redim everytime i create a new label and store the event process. I create over 8000 labels in a grid basically so its quite slow as you can imagine so anything to speed this up will really help. Thanks!

Class Module:
Code:
Option Explicit


Public WithEvents New_Label As MSForms.Label


Public Sub New_Label_Click()


Select Case Left(New_Label.Tag, 13)


    'Upload info
    Case "USR_Clickable"
        OpenFile New_Label.Caption
    
End Select


End Sub

Module:
Code:
Dim DynamicControl() As New DynamicControls
Public NewControls As New Collection



Public Sub CreateLabel(FRAME As MSForms.FRAME, Label_Caption As String, Top_Position As Integer, Left_Position As Integer, Width_Value As Integer, FixedWidth As Boolean, Height_Value As Double, TextAlignmentVar As Long, Visible_Value As Boolean, TagValue As String, UnderLineLink As Boolean, ColorChoice As Long, BackColorChoice As Long, Transparency As Boolean, FontBold As Boolean, Optional ByVal PictureImage As String)
    
    Dim Control As MSForms.Label
    
    Set Control = FRAME.Controls.Add("Forms.Label.1", , True)
       
    With Control
        .AutoSize = False
        .Width = Width_Value
        If FixedWidth Then
            .Caption = Label_Caption
            .AutoSize = False
        Else
            .Caption = Label_Caption
            .AutoSize = True
        End If
        .Left = Left_Position
        .Top = Top_Position
        .ForeColor = ColorChoice
        .BackColor = BackColorChoice
        .Height = Height_Value
        .Font.Underline = UnderLineLink
        .FontName = "Lucida Console"
        .Tag = TagValue
        If Transparency = True Then
            .BackStyle = fmBackStyleTransparent
        Else
            .BackColor = BackColorChoice
        End If
        .TextAlign = TextAlignmentVar
        .Visible = Visible_Value
        If FontBold = True Then .Font.Bold = True
        If Not PictureImage = "" Then
            .Picture = LoadPicture(ImagePathDirectory & PictureImage)
            .PicturePosition = fmPicturePositionCenter
        End If
    End With
    
    'Update Counter
    Control_Count = Control_Count + 1
    
    'Set the control to array event
    ReDim Preserve DynamicControl(1 To Control_Count)
    Set DynamicControl(Control_Count).New_Label = Control


    'Assign New Control to Collection
    NewControls.Add Control, Control.Name
    
End Sub


Public Sub RemoveControls()


    DoEvents
    Do While NewControls.count > 0
        MMCARMS.Controls.Remove NewControls.item(1).Name
        NewControls.Remove 1
    Loop
    
    Control_Count = 0
    
End Sub
 
Last edited:

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
How do you assign an event similar to what is done for the array, but to the collection? Can you provide an example or modify the code I provided above?
 
Upvote 0
I added the following in place of the redim for the previous array and I draw an error for he remove function at line : MMCARMS.Controls.Remove NewControls.item(1).Name and says object doesnt support this option or method.

Code:
    Set DynamicControl = New DynamicControls
    Set DynamicControl.New_Label = Control
    NewControls.Add DynamicControl, Control.Name
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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