Attaching same event handler to dynamically generated LinkLabels

V

Voodoo BBQ

Hello, I am dynamically generating a number of linklabels based on some
client data. This is in a plain VB class, not a Form

I am declaring my linklabel as:

Friend WithEvents linkLabel As System.Windows.Forms.LinkLabel


Then I loop through some data grabbing the link title and adding it to a
panel :

Public Sub createListOnPanel(ByVal cp As ClientProcessor, _
ByVal btnClientFormsMode As System.Windows.Forms.Button, _
ByRef displayPanel As System.Windows.Forms.Panel)
Dim cfi As ClientFormItem
Dim startXCoord As Int16 = 5
Dim startYCoord As Int16 = btnClientFormsMode.Height + 25
Dim IEnumerator As IDictionaryEnumerator = cp.FormsList.GetEnumerator()
While IEnumerator.MoveNext()
cfi = CType(IEnumerator.Value, ClientFormItem)
If Not cfi.ItemStatus = formStatus.NOT_APPLICABLE Then
linkLabel = New LinkLabel()
linkLabel.Text = cfi.ItemName
If cfi.ItemStatus = formStatus.COMPLETE Then
linkLabel.LinkColor = Color.Green
ElseIf cfi.ItemStatus = formStatus.INCOMPLETE Then
linkLabel.LinkColor = Color.Red
End If
linkLabel.Location = New System.Drawing.Point(startXCoord,
startYCoord)
linkLabel.Size = New Size(100, 20)
displayPanel.Controls.Add(linkLabel)
startYCoord += 25
End If
End While
End Sub

My event handler is:
Private Sub LinkLabel_LinkClicked(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles
linkLabel.LinkClicked
MessageBox.Show("Link was clicked")
End Sub

Everything works out except only the last generated link reacts to the mouse
click. Do I need a separate event handler and separate object per generated
linklabel? I would like to "attach" the event handler to the object before
it goes out of scope in the loop
Thanks for any help or suggestions.
S
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top