PDA

View Full Version : Solved: Create on the fly labels with click events



ly47
04-02-2010, 11:30 AM
Hi
In the attached code only the last label will recieve a a click event.
How to improve code so that every label will react to mouse click ?
Thanls
ly
code:for UserForm1

Dim WithEvents lbl As MSForms.Label

Private Sub UserForm_Initialize()
For i = 1 To 3
Set lbl = UserForm1.Controls.Add("Forms.label.1")
With lbl
.Top = i * 20
.Left = 0
.BorderStyle = 1
.Caption = "Hey" & i
.Name = "lbl" & i
End With
Next
End Sub

Private Sub lbl_Click()
MsgBox ("Bye " & lbl.Caption) ' this works
End Sub

Private Sub lbl2_Click()
MsgBox "Bye" 'not working
End Sub

Private Sub lbl3_Click()
MsgBox ("Bye") 'not working
End Sub

Bob Phillips
04-02-2010, 01:17 PM
Create a class module, call it UserFormEvents, and add this code



Option Explicit

Public WithEvents mLabelGroup As MSForms.Label

Private Sub mLabelGroup_Click()
MsgBox mLabelGroup.Caption & " has been pressed"
End Sub


and this in your form



Private Sub UserForm_Initialize()
Dim cLblEvents As UserFormEvents
Dim lbl As MSForms.Label
Dim i As Long

Set mcolEvents = New Collection
For i = 1 To 3
Set lbl = UserForm1.Controls.Add("Forms.label.1")
With lbl
.Top = i * 20
.Left = 0
.BorderStyle = 1
.Caption = "Hey" & i
.Name = "lbl" & i
End With
Set cLblEvents = New UserFormEvents
Set cLblEvents.mLabelGroup = lbl
mcolEvents.Add cLblEvents
Next

End Sub


and remove the lbl Click events.

ly47
04-02-2010, 11:22 PM
Hi
Thanks for the improved code.
It did not work,but after I add to Form declaration:
Dim mcolEvents As Collection
It works
Thank again
ly

Bob Phillips
04-03-2010, 02:27 AM
Oops, sorry I missed that bit.

mikerickson
04-03-2010, 01:03 PM
This can be done without a seperate class module.

in the userform's code module
Public WithEvents uniLabel As MSForms.Label
Dim unifiedLabels As Collection

Private Sub uniLabel_Click()
MsgBox uniLabel.Caption & " has been pressed"
End Sub

Private Sub UserForm_Activate()
Dim subUF As Object
Dim lbl As MSForms.Label
Dim i As Long
For i = 1 To 3
Set lbl = UserForm1.Controls.Add("Forms.label.1")
With lbl
.Top = i * 20
.Left = 0
.BorderStyle = 1
.Caption = "Hey" & i
.Name = "lbl" & i
End With

Set subUF = UserForms.Add(Me.Name)
Set subUF.uniLabel = lbl
unifiedLabels.Add Item:=subUF, key:=CStr(UserForms.Count)
Next
End Sub

Private Sub UserForm_Initialize()
Set unifiedLabels = New Collection
End Sub

Private Sub UserForm_Terminate()
Dim oneUF As Object
For Each oneUF In unifiedLabels
Unload oneUF
Set oneUF = Nothing
Next oneUF
Set unifiedLabels = Nothing
End Sub

ly47
04-03-2010, 03:17 PM
Hi Mike
Thanks
I tried it and it works.
Thanks again
ly

Bob Phillips
04-03-2010, 04:04 PM
But it is not proper code abstraction!

mikerickson
04-03-2010, 06:41 PM
I should mention that in the Sub uniLabel_Click, the keyword Me should not be used, neither should an explicit reference like UserForm1.TextBox1.

uniLabel.Parent should be used to refer to the visible userform and its controls, e.g. uniLabel.Parent.TextBox1


xLD,
What is "Proper code abstraction"?