473,788 Members | 2,814 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 = ConfigurationSe ttings.AppSetti ngs("cad_direct ory")

Dim strFile As String
Dim i As Integer = 0

For Each strFile In Directory.GetFi les(strDirector y) 'get the files
in the directory
If Path.GetExtensi on(strFile).ToL ower = ".doc" Or
Path.GetExtensi on(strFile).ToL ower = ".pdf" Then
Dim strTemp As String = Path.GetFileNam e(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.Contr ols.Add(lnkLabe l(i))
End If
i += 1
Next

What am I doing wrong?
--
TC
Feb 20 '06 #1
5 1115
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 = ConfigurationSe ttings.AppSetti ngs("cad_direct ory")

Dim strFile As String
Dim i As Integer = 0

For Each strFile In Directory.GetFi les(strDirector y) 'get the files
in the directory
If Path.GetExtensi on(strFile).ToL ower = ".doc" Or
Path.GetExtensi on(strFile).ToL ower = ".pdf" Then
Dim strTemp As String = Path.GetFileNam e(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.Contr ols.Add(lnkLabe l(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 = ConfigurationSe ttings.AppSetti ngs("cad_direct ory")

Dim strFile As String
Dim i As Integer = 0

For Each strFile In Directory.GetFi les(strDirector y) 'get the files
in the directory
If Path.GetExtensi on(strFile).ToL ower = ".doc" Or
Path.GetExtensi on(strFile).ToL ower = ".pdf" Then
Dim strTemp As String = Path.GetFileNam e(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.Contr ols.Add(lnkLabe l(i))
End If
i += 1
Next

What am I doing wrong?
--
TC

Feb 20 '06 #3
Terrance wrote:
Dim strDirectory = ConfigurationSe ttings.AppSetti ngs("cad_direct ory")

Dim strFile As String
Dim i As Integer = 0

For Each strFile In Directory.GetFi les(strDirector y) 'get the files
in the directory
If Path.GetExtensi on(strFile).ToL ower = ".doc" Or
Path.GetExtensi on(strFile).ToL ower = ".pdf" Then
Dim strTemp As String = Path.GetFileNam e(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.Contr ols.Add(lnkLabe l(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 =
ConfigurationSe ttings.AppSetti ngs("cad_direct ory")
Dim strFile As String
Dim i As Integer = 0

'New code here
Dim aFiles() As String = Directory.GetFi les(strDirector y)
Dim lnkLabel(aFiles .Length - 1) As LinkLabel

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

Me.lbdocs.Contr ols.Add(lnkLabe l(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.GetFi les(strDirector y)
If Path.GetExtensi on(strFile).ToL ower = ".doc" Or
Path.GetExtensi on(strFile).ToL ower = ".pdf" Then
strTemp = Path.GetFileNam e(strFile)
lnkLabel = New System.Windows. Forms.LinkLabel
With lnkLabel
.Name = "lnkLabel_" & (i)
.Text = strTemp
End With

Me.lbdocs.Contr ols.Add(lnkLabe l)
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;*.pd f" 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 = ConfigurationSe ttings.AppSetti ngs("cad_direct ory")

Dim strFile As String
Dim i As Integer = 0

For Each strFile In Directory.GetFi les(strDirector y) 'get the files
in the directory
If Path.GetExtensi on(strFile).ToL ower = ".doc" Or
Path.GetExtensi on(strFile).ToL ower = ".pdf" Then
Dim strTemp As String = Path.GetFileNam e(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.Contr ols.Add(lnkLabe l(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 =
ConfigurationSe ttings.AppSetti ngs("cad_direct ory")
Dim strFile As String
Dim i As Integer = 0

'New code here
Dim aFiles() As String = Directory.GetFi les(strDirector y)
Dim lnkLabel(aFiles .Length - 1) As LinkLabel

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

Me.lbdocs.Contr ols.Add(lnkLabe l(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
995
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 refresh all the controls and a black rectangle comes into place of each of the linklabels before they refresh. Since this is very ugly, I'm looking for way to get rid of this.
0
1088
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
2929
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 a WebForm with some textboxes, dropdownlists, a panel, imagebutton and so on. When I click on the image button (which was created at design time) I dynamically build a table. In each of row of that new table I put several cells and one cell...
7
1737
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 create the objects (which is easely fixed by calling Invoke()). Also, is there a way to acces the thread which created the thread, or in other words: is there a parent/child relationship between threads?
9
7129
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 been closed I am using this control in a Windows-Service I've developed. It works hoever the problem I'm having is that the file does not seem to be available when I receive this event. If I include a delay of a few seconds using Sleep(3000) for...
1
1301
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 Close link is showing up on the first line and covering up the file names when the box is populated. Here is the code: Dim docArray As New ArrayList() 'create an arraylist to hld the documents. Dim strDirectory =...
0
1117
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 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?...
3
1615
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 to answer the most fundamental question that every human being asks himself at some point in his or her lifetime. Not just Muslims, but every single human being.
16
4175
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 therefore being able to modify its properties?
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10173
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9967
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4070
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3674
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.