Some time ago, Reverend Jim gave me some good advice on dynamically creating controls with VB.net. Following the advice, I created 26 Labels (with text A-Z) and a event handler for the click event of the labels. Now I need to determine WHICH label was clicked and the text of that specific label. Is there a way to do this? This is NOT for a school project. This is the code I have thus far:

Public Class Form1
    Inherits System.Windows.Forms.Form
Public mylbl(26) As Label

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim lbl As Label

        For x As Integer = 0 To 25
            lbl = New Label
            lbl.Size = New Drawing.Size(25, 25)
            lbl.Name = "Label" & x
            lbl.Location = New Point(20 * (x + 1), 25)
            lbl.ForeColor = System.Drawing.Color.Blue
            lbl.TextAlign = HorizontalAlignment.Center
            lbl.Text = Chr(65 + x)

            mylbl(x) = lbl
            GroupBox1.Controls.Add(lbl)
            AddHandler mylbl(x).Click, AddressOf Label_Click


        Next
End Sub

Private Sub Label_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


End Sub

major_lost

"Sender" typically is the control prompting the call to the event handler method. Cast it to the type of control you hope it is after a bit of chgecking first.

   Private Sub Label_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
      If sender IsNot Nothing AndAlso TypeOf sender Is Label Then
         Dim lbl As Label = CType(sender, Label)
         MsgBox(lbl.Text)
      End If
   End Sub

When I copy and paste this exact code into the event handler for the label, I get the following error:
"end of statement expected"
major_lost

Are you sure you copied everything, did you overwrite the stub you already started, did you add the missing End Class statement at the end of your code? The code works fine as presented. Here it is all together:

Public Class Form1
    Inherits System.Windows.Forms.Form
    Public mylbl(26) As Label
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim lbl As Label
        For x As Integer = 0 To 25
            lbl = New Label
            lbl.Size = New Drawing.Size(25, 25)
            lbl.Name = "Label" & x
            lbl.Location = New Point(20 * (x + 1), 25)
            lbl.ForeColor = System.Drawing.Color.Blue
            lbl.TextAlign = HorizontalAlignment.Center
            lbl.Text = Chr(65 + x)
            mylbl(x) = lbl
            GroupBox1.Controls.Add(lbl)
            AddHandler mylbl(x).Click, AddressOf Label_Click
        Next
    End Sub
    Private Sub Label_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If sender IsNot Nothing AndAlso TypeOf sender Is Label Then
            Dim lbl As Label = CType(sender, Label)
            MsgBox(lbl.Text)
        End If
    End Sub
End Class

Hi,

Save the Index of the label Control, in the Tag Property...
Write this line at Line 16

lbl.Tag = x

In Click Event.. and to get the Index, use:

MsgBox(CType(sender, Label).Tag)

Regards
Veena

Thanks to the both of you. Tinstaafl, you were correct, I was missing the "end class" in the post, but not the code. The problem still exists with the IF statement:

If sender IsNot Nothing AndAlso TypeOf sender Is Label Then

Version 1.1 must not like the "IsNot" because it will not accept it.

major_lost

How about If not IsNothing(sender) AndAlso ....?

You can do it without the If statement, it's basically redundant. Only clicking a label will call the code, so sender will always be a label, as long as you don't add the handler to the click event of a different type of control.

Thanks everyone - I got it to work!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.