364,112 Members | 2322 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

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

!NoItAll
100+
P: 222
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
Expand|Select|Wrap|Line Numbers
  1. Addhandler label(I).Click, AddressOf ShowPix
  2.  
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

Expand|Select|Wrap|Line Numbers
  1. Private Sub ShowPix()
  2.     'how do I determine which dynamically created label sent the click event...
  3. End Sub
  4.  
  5.  
So how do I determine which label the click event came from?

I have tried Me.ActiveControl.tag (I previously set the tag for each label appropriately - but that doesn't always work.
Jan 28 '12 #1

✓ answered by jhirsch41

using the correct signature of a click handler solves the problem, for example:
Expand|Select|Wrap|Line Numbers
  1. Private Sub ShowPix(Byval sender as Object, Byval e as EventArgs)
  2.      Dim lbl as Label = CType(sender, Label)
  3.      Dim lblName as String = lbl.Name 
  4. End Sub
  5.  
etc.......
Share this Question
Share on Google+
2 Replies


jhirsch41
P: 1
using the correct signature of a click handler solves the problem, for example:
Expand|Select|Wrap|Line Numbers
  1. Private Sub ShowPix(Byval sender as Object, Byval e as EventArgs)
  2.      Dim lbl as Label = CType(sender, Label)
  3.      Dim lblName as String = lbl.Name 
  4. End Sub
  5.  
etc.......
Jan 28 '12 #2

!NoItAll
100+
P: 222
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!
Jan 28 '12 #3

Post your reply

Help answer this question



Didn't find the answer to your Visual Basic .NET question?

You can also browse similar questions: Visual Basic .NET