Consulting

Results 1 to 8 of 8

Thread: Solved: Create on the fly labels with click events

  1. #1
    VBAX Regular
    Joined
    Apr 2010
    Posts
    6
    Location

    Solved: Create on the fly labels with click events

    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

    [VBA]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[/VBA]

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,446
    Location
    Create a class module, call it UserFormEvents, and add this code

    [vba]

    Option Explicit

    Public WithEvents mLabelGroup As MSForms.Label

    Private Sub mLabelGroup_Click()
    MsgBox mLabelGroup.Caption & " has been pressed"
    End Sub
    [/vba]

    and this in your form

    [vba]

    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
    [/vba]

    and remove the lbl Click events.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Apr 2010
    Posts
    6
    Location
    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

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,446
    Location
    Oops, sorry I missed that bit.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,773
    This can be done without a seperate class module.

    in the userform's code module
    [VBA]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
    [/VBA]

  6. #6
    VBAX Regular
    Joined
    Apr 2010
    Posts
    6
    Location
    Hi Mike
    Thanks
    I tried it and it works.
    Thanks again
    ly

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,446
    Location
    But it is not proper code abstraction!
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,773
    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"?
    Last edited by mikerickson; 04-03-2010 at 06:58 PM.

Posting Permissions

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