|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
(newbie-VB2005 EE) - filenames in ListBoxHi, 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) 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 ;-) -- Show quoteHide quoteProgramming 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) "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) Peter Proost schreef:
> Hi this should do it, mind the typo's as it's out of my head: Yep it works .... Thanks.> > ListBox1.Items.AddRange(Directory.GetFiles("c:\test")) > > Hope this helps > > Peter also from Flanders/Belgium/Europe ;-) Vito 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) Olie schreef:
> I guess the reason a listbox is seldom used for this task is that most I changed the code to this:> 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 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
Show quote
Hide quote
"Opa Vito" <gebr***@dit.niet> schrieb: Check out 'System.IO.Path' and its shared methods too!>> 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. -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Newbee - Project with single module.
force cast of object to mytype... How to ensure DB changes complete? Method Address from MethodInfo: How do I get it? mouse freezes in Visual Studio Threading Can't access controls programmatically when inside FormView control Service not going to 'Started' Gridview question How can I control positioning of child windows in an MDI form? |
|||||||||||||||||||||||