Hello, I was wondering if anyone can help me. I have some code where I'm
creating linklabels during runtime using filenames that in a directory. I
created a LinkClicked event but it only launches the last file that is called
in the ClickEvent. I tried placing the Event in the FOR EACH statement but
that's not helping. If interested I'm using VB2005. How can I apply a Click
Event to each LinkLabel or each linklabel to the same ClickedEvent? Can
someone help? Here is the code:
Dim mLinkLabel As LinkLabel 'declared globally
Private Sub CreateLinkLabels(ByRef strDirectory As String)
Dim strTmp, strFile As String
<Other variable declarations>
For Each strFile In Directory.GetFiles(strDirectory) 'grab each file
in the directory
If Path.GetExtension(strFile).ToLower = ".doc" Or
Path.GetExtension(strFile).ToLower = ".pdf" Then
strTmp = Path.GetFileName(strFile)
mLinkLabel = New LinkLabel
With mLinkLabel
.Name = "lnkLabel_" & i.ToString()
.Visible = True
.ActiveLinkColor = Color.Red
.DisabledLinkColor = Color.Blue
.LinkColor = Color.Blue
.VisitedLinkColor = Color.Purple
.Size = New System.Drawing.Size(intWidth, intHeight)
.Location = New System.Drawing.Point(intX, intY *
intIncrement)
.Text = strTmp
End With
AddHandler mLinkLabel.LinkClicked, AddressOf
mLinkLabel_LinkClicked 'addhandler for click event.
intNum += 1
intIncrement += 2
'Add the linkLabels to the panel control.
plMain.Controls.Add(mLinkLabel)
End If
i += 1
Next
tsText.Text = intNum.ToString() & " Files"
End Sub
Private Sub mLinkLabel_LinkClicked(ByVal sender As Object, ByVal e As
LinkLabelLinkClickedEventArgs) 'Handles myLinkLabel.LinkClicked
Dim lnk As LinkLabel = New LinkLabel
lnk = CType(sender, LinkLabel)
lnk.Links(lnk.Links.IndexOf(e.Link)).Visited = True
System.Diagnostics.Process.Start("C:\Documents and Settings
Documents\test\" & mLinkLabel.Text)
End Sub
--
TC