Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27153

VS 2010 [RESOLVED] How to detect which label was clicked in common eventhandler

$
0
0
I design labels dynamically and add them to a Panel. I use AddHandler to create a single handler to deal with them all and the handler works. If I click a label, it shows the MessageBox.
However, I want to identify WHICH label was clicked. It should be as easy as
vb.net Code:
  1. Dim MyLabel as New Label
  2. MyLabel = sender
  3. MessageBox.Show (MyLabel.Name & " clicked")
However, this gives the error: Option Strict On disallows implicit conversion from Object to System.Windows.Forms.Label
The only options Intellisense gives me when I do sender. are: Equals, GetHashCode, GetType, ReferenceEquals, ToString.
None of which help me. What am I doing wrong please?
My code is below:
vb.net Code:
  1. Private Sub LoadCalendarLabels()
  2.         Dim Counter As Integer = 0
  3.         For g As Integer = 1 To 15
  4.             For f As Integer = 1 To 7
  5.                 Counter += 1
  6.                 Dim NewLabel As Label = New Label
  7.                 With NewLabel
  8.                     .AutoSize = False
  9.                     .BorderStyle = BorderStyle.FixedSingle
  10.                     .BackColor = System.Drawing.SystemColors.Control
  11.                     .Parent = Panel1
  12.                     .Size = New System.Drawing.Size(35, 35)
  13.                     .Location = New System.Drawing.Point(5 + ((f - 1) * 39), 30 + ((g - 1) * 39))
  14.                     .Tag = Counter
  15.                     .Name = "Calendar" & Counter.ToString
  16.                     .Font = New Font("comic sans", 14, FontStyle.Bold)
  17.                     .Visible = True
  18.                     .TextAlign = ContentAlignment.MiddleCenter
  19.                     .Text = Counter.ToString
  20.                     AddHandler NewLabel.Click, AddressOf Calendar_Click
  21.                 End With
  22.             Next
  23.         Next
  24.     End Sub
  25.  
  26.  
  27.     Private Sub Calendar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  28.         MsgBox("clicked")
  29.  
  30.         'This doesn't work!
  31.         'Dim myLabel As New Label
  32.         'myLabel = sender
  33.     End Sub

Viewing all articles
Browse latest Browse all 27153

Trending Articles