Dynamically Create labels with click events and dynamically delete labels

bradyboyy88

Well-known Member
Joined
Feb 25, 2015
Messages
562
I have the following code so far which uses arrays to add click events for newly generated labels. However, I want to convert this to using collections instead since I have to redim preserve everytime a button is made which is supposively a performance hog. I have tried to convert over to collection but passing the event doesnt seem to do anything for the older labels created. So using this code could someone help point me in the right direcftino for correction using collections instead of array. Also, how do I delete these labels dynamically? I need to do that so they dont just keep adding up since the screen it generates on is open most of the day using new queries and such.

module
Code:
Option Explicit
 
Dim DynamicControl() As New DynamicControls


Sub CreateLabelUMD(FRAME As MSForms.FRAME, Label_Caption As String, Top_Position As Integer, Left_Position As Integer, Width_Value As Integer, Visible_Value As Boolean)
    
    Dim Control As MSForms.Label
    
    Set Control = FRAME.Controls.Add("Forms.Label.1", , True)
 
    With Control
        .AutoSize = True
        .Width = Width_Value
        .Caption = Label_Caption
        .Left = Left_Position
        .Top = Top_Position
        .ForeColor = &HFF0000
        .Font.Underline = True
        .Visible = Visible_Value
    End With


    ReDim Preserve DynamicControl(1 To i)
    Set DynamicControl(i).New_Label = Control
    i = i + 1
    
End Sub
Sub RemoveLabelsUMD(FRAME As MSForms.FRAME)


While i > 0


    FRAME.Controls.Remove DynamicControl(i)
    i = i - 1
Loop


End Sub

class module
Code:
Option Explicit


Public WithEvents New_Label As MSForms.Label


Public Sub New_Label_Click()


OpenFile New_Label.Caption


End Sub
 
Last edited:

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Have you considered adding the necessary controls to your userform at design time, and making them visible when required?

In any case, to use a collection instead of an array, replace...

Code:
Dim DynamicControl() As New DynamicControls

with

Code:
Dim colDynamicControls As New Collection

And then replace...

Code:
ReDim Preserve DynamicControl(1 To i)
Set DynamicControl(i).New_Label = Control
i = i + 1

with

Code:
Dim cDynCtrl As DynamicControls
Set cDynCtrl = New DynamicControls
Set cDynCtrl.New_Label = Control
colDynamicControls.Add cDynCtrl

Note that once the userform is unloaded these labels will automatically be removed from the userform.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,155
Messages
6,123,335
Members
449,098
Latest member
thnirmitha

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