How to get which dynamically created label was clicked...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • !NoItAll
    Contributor
    • May 2006
    • 297

    How to get which dynamically created label was clicked...

    I am creating multiple labels on a form dynamically. Essentially then they become an array.

    Label(0)
    Label(1)
    Label(2)
    etc...

    I am also adding a handler for each one
    Code:
    Addhandler label(I).Click, AddressOf ShowPix
    The sub "ShowPix" is common to all of them because I do not know how many labels will be created.

    So now inside of Showpix

    Code:
    Private Sub ShowPix()
        'how do I determine which dynamically created label sent the click event...
    End Sub
    So how do I determine which label the click event came from?

    I have tried Me.ActiveContro l.tag (I previously set the tag for each label appropriately - but that doesn't always work.
  • jhirsch41
    New Member
    • Jan 2012
    • 1

    #2
    using the correct signature of a click handler solves the problem, for example:
    Code:
    Private Sub ShowPix(Byval sender as Object, Byval e as EventArgs)
         Dim lbl as Label = CType(sender, Label)
         Dim lblName as String = lbl.Name 
    End Sub
    etc.......

    Comment

    • !NoItAll
      Contributor
      • May 2006
      • 297

      #3
      Thanks jhirsch41!
      That works a treat! I was playing with that a bit, but was attempting to only use the "sender as object" part - which appears to not give it the correct signature - so it was a mess. Your solution does exactly what I needed! Thanks again!

      Comment

      Working...