LinkLabels Not Being Created

G

Guest

I was wondering if someone could help me with my problem. I have some code
that gets document names from a directory; I'm trying to make each document
name into a linklabel. I got the name of the first document to show but each
additional document is not showing.

Dim strDirectory = ConfigurationSettings.AppSettings("cad_directory")

Dim strFile As String
Dim i As Integer = 0

For Each strFile In Directory.GetFiles(strDirectory) 'get the files
in the directory
If Path.GetExtension(strFile).ToLower = ".doc" Or
Path.GetExtension(strFile).ToLower = ".pdf" Then
Dim strTemp As String = Path.GetFileName(strFile)
Dim lnkLabel(i) As LinkLabel
lnkLabel(i) = New System.Windows.Forms.LinkLabel
With lnkLabel(i)
.Name = "lnkLabel_" & (i)
.Text = strTemp
End With

Me.lbdocs.Controls.Add(lnkLabel(i))
End If
i += 1
Next

What am I doing wrong?
 
G

Guest

My VB is a little rusty, but could this be a declaration problem?

Try changing:
Dim lnkLabel(i) As LinkLabel
to
Dim lnkLabel As LinkLabel '(drop the array suffix)

Also, you could (should be able to) move the declaration outside the ForEach
loop, and thus just re-initialize the lnkLabel with each iteration.

HTH,
Steve
 
G

Guest

Thanks, Steve for your suggestions; however, I'm trying to create a new
LinkLabel object for each file in the directory. If I declare the object
outside of the FOR EACH loop then it's index is set to zero for each instance
of the LinkLabel that I'm trying to create. Now, If I'm wrong will someone
please correct me?

Thanks,
 
C

Chris Dunaway

Terrance said:
Dim strDirectory = ConfigurationSettings.AppSettings("cad_directory")

Dim strFile As String
Dim i As Integer = 0

For Each strFile In Directory.GetFiles(strDirectory) 'get the files
in the directory
If Path.GetExtension(strFile).ToLower = ".doc" Or
Path.GetExtension(strFile).ToLower = ".pdf" Then
Dim strTemp As String = Path.GetFileName(strFile)
Dim lnkLabel(i) As LinkLabel
lnkLabel(i) = New System.Windows.Forms.LinkLabel
With lnkLabel(i)
.Name = "lnkLabel_" & (i)
.Text = strTemp
End With

Me.lbdocs.Controls.Add(lnkLabel(i))
End If
i += 1
Next

What am I doing wrong?
Dim lnkLabel(i) As LinkLabel

The problem is the line above. You are declaring a new lnkLabel array
every time through the loop. You need to declare the array outside the
loop like this:

Dim strDirectory =
ConfigurationSettings.AppSettings("cad_directory")
Dim strFile As String
Dim i As Integer = 0

'New code here
Dim aFiles() As String = Directory.GetFiles(strDirectory)
Dim lnkLabel(aFiles.Length - 1) As LinkLabel

For Each strFile In aFiles 'get the files in the directory
If Path.GetExtension(strFile).ToLower = ".doc" Or
Path.GetExtension(strFile).ToLower = ".pdf" Then
Dim strTemp As String = Path.GetFileName(strFile)
lnkLabel(i) = New System.Windows.Forms.LinkLabel
With lnkLabel(i)
.Name = "lnkLabel_" & (i)
.Text = strTemp
End With

Me.lbdocs.Controls.Add(lnkLabel(i))
End If
i += 1
Next

Notice that in this code, I called the GetFiles method into a string
array so I could get its length in order to create an array for the
link labels of the right size.

Hope this helps,

Chris
 
G

Guest

Unless you need the LinkLabels in a control array for some reason, you don't
need the index at all, but either way, the index is still managed inside the
loop. See if the code below helps you any.

Dim strFile As String
Dim i As Integer = 0
Dim strTemp As String
Dim lnkLabel As LinkLabel

For Each strFile In Directory.GetFiles(strDirectory)
If Path.GetExtension(strFile).ToLower = ".doc" Or
Path.GetExtension(strFile).ToLower = ".pdf" Then
strTemp = Path.GetFileName(strFile)
lnkLabel = New System.Windows.Forms.LinkLabel
With lnkLabel
.Name = "lnkLabel_" & (i)
.Text = strTemp
End With

Me.lbdocs.Controls.Add(lnkLabel)
End If
i += 1
Next

A totally different approach would be to simply get the file list into
anything that supports IList and then databind the collection to a server
control. The advantage to this method is you have less code and a
declarative model for more flexible display options. The article below is an
example doing pretty much what you want to do, using a DataGrid for display.
If the datagrid isn't to your liking, you could use a Repeater, etc. I can't
remember if the GetFiles method accepts multiple extensions, but if it does
it's probably in the form of "*.doc;*.pdf" or something simple like that. If
it doesn't you could always create two arrays and combine them before
databinding.

http://aspnet.4guysfromrolla.com/articles/052803-1.aspx

Steve
 
G

Guest

Thanks Chris and Steve for your help. I greatly appreciate it. I will try
both methods.

Again thanks a bunch!
 

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