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
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:
However, I want to identify WHICH label was clicked. It should be as easy as
vb.net Code:
Dim MyLabel as New Label MyLabel = sender MessageBox.Show (MyLabel.Name & " clicked")
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:
Private Sub LoadCalendarLabels() Dim Counter As Integer = 0 For g As Integer = 1 To 15 For f As Integer = 1 To 7 Counter += 1 Dim NewLabel As Label = New Label With NewLabel .AutoSize = False .BorderStyle = BorderStyle.FixedSingle .BackColor = System.Drawing.SystemColors.Control .Parent = Panel1 .Size = New System.Drawing.Size(35, 35) .Location = New System.Drawing.Point(5 + ((f - 1) * 39), 30 + ((g - 1) * 39)) .Tag = Counter .Name = "Calendar" & Counter.ToString .Font = New Font("comic sans", 14, FontStyle.Bold) .Visible = True .TextAlign = ContentAlignment.MiddleCenter .Text = Counter.ToString AddHandler NewLabel.Click, AddressOf Calendar_Click End With Next Next End Sub Private Sub Calendar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) MsgBox("clicked") 'This doesn't work! 'Dim myLabel As New Label 'myLabel = sender End Sub