473,395 Members | 2,151 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

LinkLabels Not Being Created

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?
--
TC
Feb 20 '06 #1
5 1106
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
"Terrance" wrote:
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?
--
TC

Feb 20 '06 #2
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,
--
TC
"Steve" wrote:
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
"Terrance" wrote:
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?
--
TC

Feb 20 '06 #3
Terrance wrote:
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

Feb 20 '06 #4
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

Feb 20 '06 #5
Thanks Chris and Steve for your help. I greatly appreciate it. I will try
both methods.

Again thanks a bunch!
--
TC
"Chris Dunaway" wrote:
Terrance wrote:
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

Feb 20 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: HamuNaptra | last post by:
Hi, I have a form which contains more than 15 linklabels, all of them have a transparant backcolor. The form itself has a backgroundimage. Now when I show the form, it takes a moment to...
0
by: Wajih | last post by:
I have a list view. I want to insert LinkLabels dynamically in it as ListViewSubItems. Is it possible to do this? If yes, how should i do it. If no, are there any workarounds. Thanx and regards
2
by: R Duke | last post by:
I have tried everything I can think of to change the visible property of a design time created control from a dynamically created control's command event handler. Here is the scenario. I have...
7
by: MariusI | last post by:
Are objects implicitly stored in the TLS of the currently running thread? When creating multithreaded applications i get errors when accessing data from a different thread than the thread used to...
9
by: Tushar | last post by:
Followup-To: microsoft.public.dotnet.general Does anyone know when is this event raised, is it: 1) When the file is created but may not have been closed 2) When the file is created AND it has...
1
by: Terrance | last post by:
I have a problem I was hoping someone can help me with. My first problem is moving a link label that is created at runtime to the of an arraylist on the next line in the listbox. Currently, the...
0
by: Terrance | last post by:
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...
3
by: Happy Man | last post by:
Why we created All praise is due to Allah and may His peace and blessings be upon his last messenger (saaw) and on all those who follow the path of righteousness until the last day. We have...
16
by: Mike | last post by:
Hi, I have a form with some controls, and a different class that needs to modify some control properties at run time. Hoy can I reference the from so I have access to its controls and...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.