Home All Groups Group Topic Archive Search About

(newbie-VB2005 EE) - filenames in ListBox

Author
18 Sep 2006 12:40 PM
Opa Vito
Hi, my project needs a way to list the filenames of files of a certain
folder. To my understanding a ListBox is best for this purpose. It lists
the filenames and one can select a file and pass this name to a
string-variable for further use. That's what I intend to do anyway.

I found many examples of binding a ListBox to a database but none to a
folder .... for some strange reason it seems seldom to be used this way.
So here's my question:
How do I 'connect' a Listbox to a folder and read the filenames?

Many thanks

Vito
(from Flanders/Belgium/Europe)

Author
18 Sep 2006 12:49 PM
Peter Proost
Hi this should do it, mind the typo's as it's out of my head:

ListBox1.Items.AddRange(Directory.GetFiles("c:\test"))

Hope this helps

Peter also from Flanders/Belgium/Europe ;-)

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)

Show quoteHide quote
"Opa Vito" <gebr***@dit.niet> schreef in bericht
news:Ir2dnYGsvsxIDpPYRVny1Q@scarlet.biz...
> Hi, my project needs a way to list the filenames of files of a certain
> folder. To my understanding a ListBox is best for this purpose. It lists
> the filenames and one can select a file and pass this name to a
> string-variable for further use. That's what I intend to do anyway.
>
> I found many examples of binding a ListBox to a database but none to a
> folder .... for some strange reason it seems seldom to be used this way.
> So here's my question:
> How do I 'connect' a Listbox to a folder and read the filenames?
>
> Many thanks
>
> Vito
> (from Flanders/Belgium/Europe)
Author
18 Sep 2006 1:26 PM
Opa Vito
Peter Proost schreef:
> Hi this should do it, mind the typo's as it's out of my head:
>
>  ListBox1.Items.AddRange(Directory.GetFiles("c:\test"))
>
> Hope this helps
>
> Peter also from Flanders/Belgium/Europe ;-)

Yep it works .... Thanks.

Vito
Author
18 Sep 2006 12:54 PM
Olie
I guess the reason a listbox is seldom used for this task is that most
people use the OpenFileDialog control.

You may be able to directly bind to a folder using a list box but I
would just simply iterate through the files in the folder and add them
to the listbox.

dir1 = New DirectoryInfo(DirPath)
For Each Item As FileInfo In dir1.GetFiles()
listbox.Items.Add(Item.ShortName)
Next



Opa Vito wrote:
Show quoteHide quote
> Hi, my project needs a way to list the filenames of files of a certain
> folder. To my understanding a ListBox is best for this purpose. It lists
> the filenames and one can select a file and pass this name to a
> string-variable for further use. That's what I intend to do anyway.
>
> I found many examples of binding a ListBox to a database but none to a
> folder .... for some strange reason it seems seldom to be used this way.
> So here's my question:
> How do I 'connect' a Listbox to a folder and read the filenames?
>
> Many thanks
>
> Vito
> (from Flanders/Belgium/Europe)
Author
18 Sep 2006 1:35 PM
Opa Vito
Olie schreef:
> I guess the reason a listbox is seldom used for this task is that most
> people use the OpenFileDialog control.
>
> You may be able to directly bind to a folder using a list box but I
> would just simply iterate through the files in the folder and add them
> to the listbox.
>
> dir1 = New DirectoryInfo(DirPath)
> For Each Item As FileInfo In dir1.GetFiles()
> listbox.Items.Add(Item.ShortName)
> Next

I changed the code to this:

  Dim dir1 As New DirectoryInfo("c:\pictures")
  For Each Item As FileInfo In dir1.GetFiles()
  ListBox1.Items.Add(Item.Name.Substring(0, Item.Name.IndexOf(".")))
  Next

and it works.

'ShortName' doesn't seem to be a member of File:IO, but please
understand I'm a newbie in programming so maybe I didn't get a few
things clear :-)

Vito
Author
18 Sep 2006 9:50 PM
Herfried K. Wagner [MVP]
Show quote Hide quote
"Opa Vito" <gebr***@dit.niet> schrieb:
>> I guess the reason a listbox is seldom used for this task is that most
>> people use the OpenFileDialog control.
>>
>> You may be able to directly bind to a folder using a list box but I
>> would just simply iterate through the files in the folder and add them
>> to the listbox.
>>
>> dir1 = New DirectoryInfo(DirPath)
>> For Each Item As FileInfo In dir1.GetFiles()
>> listbox.Items.Add(Item.ShortName)
>> Next
>
> I changed the code to this:
>
>  Dim dir1 As New DirectoryInfo("c:\pictures")
>  For Each Item As FileInfo In dir1.GetFiles()
>  ListBox1.Items.Add(Item.Name.Substring(0, Item.Name.IndexOf(".")))
>  Next
>
> and it works.

Check out 'System.IO.Path' and its shared methods too!

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>