|
-
Jun 21st, 2009, 01:36 AM
#1
Thread Starter
Member
add all files from a folder to a listbox?
can anyone help me with this? ive been looking at examples and trying to figure it out for 2 hours....
-
Jun 21st, 2009, 02:01 AM
#2
New Member
Re: add all files from a folder to a listbox?
there are three controls can do this , the first is DriveListBox control ,the second is DirListBox control. the last one is FileListBox. if you want get all the file name ,you need link all of them as a combox. you need add some code with the change event.
you can get more information by search with MSDN.
-
Jun 21st, 2009, 02:56 AM
#3
Re: add all files from a folder to a listbox?
What's the purpose? It's pretty easy to show the commondialog control's file browser.
Otherwise, the controls mentioned above are in the toolbox.
-
Jun 21st, 2009, 11:40 AM
#4
Thread Starter
Member
Re: add all files from a folder to a listbox?
i'm trying to make it so that when the program start it reads the title of all the music files in my "music folder" and displays the titles in a list...so those tools don't really help me...
-
Jun 21st, 2009, 12:07 PM
#5
Re: add all files from a folder to a listbox?
 Originally Posted by hans4
i'm trying to make it so that when the program start it reads the title of all the music files in my "music folder" and displays the titles in a list...so those tools don't really help me...
Actually the FileList control can help you. The only thing you really need to know is where "my music" is located. There is an API approach that will find that special folder regardless of where it is physically. In this example shows how to retrieve all special folders (as of the date of its writing). You can trim it down to a few lines of code if looking for just one folder.
Once you have the folder location, set the Path property of the filelist control. You can also use a listbox and manually fill it by using Dir() in a loop, or using APIs to fill the listbox for you. Quite a few ways to skin that cat.
-
Jun 21st, 2009, 02:57 PM
#6
Re: add all files from a folder to a listbox?
yes, the file list box could work fine for you. You just have to tell it which directory to look at.
-
Jun 21st, 2009, 07:43 PM
#7
New Member
Re: add all files from a folder to a listbox?
Most of this I got from AllApi.net Seemed to be the easiest way to locate the My Pictures folder.
To test this start a new project and add a Directory List Box, a File List Box and a Regular List Box to the form. Copy and past the following code.
Option Explicit
Const CSIDL_MYMUSIC = &HD
Private Type SHITEMID
cb As Long
abID As Byte
End Type
Private Type ITEMIDLIST
mkid As SHITEMID
End Type
Private Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" (ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As ITEMIDLIST) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
Private Sub Form_Load()
'Add a Directory List box to your form
'Add a File List Box to your form
'Add a regular List Box to your form
Dim i As Integer
Dir1.Visible = False
File1.Visible = False
Dir1.Path = GetSpecialfolder(CSIDL_MYMUSIC)
File1.Path = Dir1.Path
For i = 0 To File1.ListCount - 1
List1.AddItem File1.List(i)
Next i
End Sub
Private Function GetSpecialfolder(CSIDL As Long) As String
Dim Path As String
Dim NOERROR As Boolean
Dim r As Long
Dim IDL As ITEMIDLIST
'Get the special folder
r = SHGetSpecialFolderLocation(100, CSIDL, IDL)
If r = NOERROR Then
'Create a buffer
Path = Space(512)
'Get the path from the IDList
r = SHGetPathFromIDList(ByVal IDL.mkid.cb, ByVal Path$)
'Remove the unnecessary chr$(0)'s
GetSpecialfolder = Left$(Path, InStr(Path, Chr$(0)) - 1)
Exit Function
End If
GetSpecialfolder = ""
End Function
-
Jun 21st, 2009, 08:20 PM
#8
Re: add all files from a folder to a listbox?
This does it:
Code:
Private Sub Command1_Click()
Dim NameFile As String, SubDir As String
SubDir = CurDir$ & "\*.*" ' Change CurDir to whatever directory is being searched
NameFile = Dir$(SubDir)
Do While NameFile <> vbNullString
List1.AddItem NameFile
NameFile = Dir$
Loop
End Sub
-
Jun 22nd, 2009, 12:39 AM
#9
Frenzied Member
Re: add all files from a folder to a listbox?
How about an API solution...
Code:
Option Explicit
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const LB_DIR = &H18D
Private Const DDL_READWRITE = &H0
Private Const DDL_READONLY = &H1
Private Const DDL_HIDDEN = &H2
Private Const DDL_SYSTEM = &H4
Private Const DDL_DIRECTORY = &H10
Private Const DDL_ARCHIVE = &H20
Private Const DDL_DRIVES = &H4000
Private Const DDL_EXCLUSIVE = &H8000&
Private Const DDL_POSTMSGS = &H2000
Private Const FindAllFiles = DDL_READWRITE Or DDL_READONLY Or DDL_HIDDEN Or DDL_SYSTEM
Private Sub Form_Load()
Dim Cnt As Integer
Me.Visible = True
Cnt = SendMessage(List1.hwnd, LB_DIR, FindAllFiles, ByVal "c:\*.*")
MsgBox "Found " & Cnt + 1 & " files"
End Sub
Good Luck
Option Explicit should not be an Option!
-
Jun 22nd, 2009, 02:58 AM
#10
Re: add all files from a folder to a listbox?
 Originally Posted by vb5prgrmr
How about an API solution...
Code:
Option Explicit
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const LB_DIR = &H18D
Private Const DDL_READWRITE = &H0
Private Const DDL_READONLY = &H1
Private Const DDL_HIDDEN = &H2
Private Const DDL_SYSTEM = &H4
Private Const DDL_DIRECTORY = &H10
Private Const DDL_ARCHIVE = &H20
Private Const DDL_DRIVES = &H4000
Private Const DDL_EXCLUSIVE = &H8000&
Private Const DDL_POSTMSGS = &H2000
Private Const FindAllFiles = DDL_READWRITE Or DDL_READONLY Or DDL_HIDDEN Or DDL_SYSTEM
Private Sub Form_Load()
Dim Cnt As Integer
Me.Visible = True
Cnt = SendMessage(List1.hwnd, LB_DIR, FindAllFiles, ByVal "c:\*.*")
MsgBox "Found " & Cnt + 1 & " files"
End Sub
Good Luck
that's actually the code i was looking for. I'd seen it on this site before.
Note to user: This is to be used with a standard listbox, not the file or dir one. In reality they are all the same control and this treats a standard listbox as a file box.
-
Jun 22nd, 2009, 07:29 AM
#11
Frenzied Member
Re: add all files from a folder to a listbox?
Yeah, I posted it about a month or so ago and couldn't find it myself with a keyword search so I posted this version.
Good Luck
Option Explicit should not be an Option!
-
Jun 22nd, 2009, 03:13 PM
#12
Re: add all files from a folder to a listbox?
 Originally Posted by vb5prgrmr
Yeah, I posted it about a month or so ago and couldn't find it myself with a keyword search so I posted this version.
Good Luck
you don't need a keyword search if you post it yourself. Your user profile has a link "find all posts started by vb5prgrmr" and you can also view all threads you are subscribed to.
-
Jun 22nd, 2009, 07:02 PM
#13
Re: add all files from a folder to a listbox?
 Originally Posted by Lord Orwell
you don't need a keyword search if you post it yourself. Your user profile has a link "find all posts started by vb5prgrmr" and you can also view all threads you are subscribed to.
Lord Orwell, please permit me to ask a question.
Why would any programmer want to use an API to do what 7 lines of typical code in VB6 (or VB3 for that matter) can probaby do easier and faster?
Just curious.
-
Jun 22nd, 2009, 07:43 PM
#14
New Member
Re: add all files from a folder to a listbox?
You can use FileListBox instead. Using DriveListBox and DirListBox is much easier.
I'll share my code to you and you can try it.
Names of my controls: DriveListBox = DriveList, DirListBox = DirList, FileListBox = FileList
Code:
Private Sub Form_Load()
FileList.Pattern = "*.mp3" 'if you would like to show only mp3 files
End Sub
Code:
Private Sub DriveList_Change()
On Error GoTo drvError
DirList.Path = DriveList.Drive
Exit Sub
drvError:
MsgBox "Drive not available.", vbCritical
End Sub
Code:
Private Sub DirList_Change()
FileList.Path = DirList.Path
End Sub

Check out website design company.
-
Jun 22nd, 2009, 10:03 PM
#15
Frenzied Member
Re: add all files from a folder to a listbox?
 Originally Posted by Code Doc
Lord Orwell, please permit me to ask a question.
Why would any programmer want to use an API to do what 7 lines of typical code in VB6 (or VB3 for that matter) can probaby do easier and faster?
Just curious. 
Because the API is actually faster because it is a direct call to the underlying functionality of most of vb's functionality, which means it is not interpreted by the visual basic runtime and then passed to the underlying functionality and the results pumped back up through the chain like the six lines of dir you are talking about.
Lord Orwell, as of so far I have 13 threads that I replied to with sendmessage in it and 345 threads in all that I have replied to. And yes I know I could do that but when you are looking for a previously posted solution based upon memory without trying to open up another application. Well sometimes you have to open up that other application to remind yourself...
Good Luck All
Option Explicit should not be an Option!
-
Jun 23rd, 2009, 02:19 AM
#16
Re: add all files from a folder to a listbox?
 Originally Posted by Code Doc
Lord Orwell, please permit me to ask a question.
Why would any programmer want to use an API to do what 7 lines of typical code in VB6 (or VB3 for that matter) can probaby do easier and faster?
Just curious. 
a couple of reasons.
1. There isn't any other solution for getting the location of the user's folder in question. Yes, there are default locations, but these locations aren't even in the same places in different operating systems. Your desktop location for example:
win95/98/me:
c:\windows\desktop (assuming they name their install windows)
2k/xp:
c:\documents and settings\users\username\desktop
vista\7:
c:\users\username\desktop
and these are merely default locations. They can be moved by the user to anywhere they wish, even other hard drives.
2. The filelist control is an activex control and if you use it instead of the standard listbox, it has to be included with the compiled program, making the distributable larger. This is especially apparent with the common dialog control. the control is a couple of megabytes in size, and about 20 lines of code can reproduce it.
3. Using the controls through api can give you more functionality than the control itself gives you through vb. Richedit comes to mind. It has link functionality that isn't used in the richtextbox control.
4. The code is actually smaller. If you strip down that code posted above, it's actually a declaration and a call and that's about it. None of the constants are required.
Code:
Cnt = SendMessage(List1.hwnd, &H18d, 7, ByVal "c:\*.*")
besides the single sendmessage declaration, that's all that is required to load the c:\ directory into the listbox, and it's much, MUCH faster than the dir$ command in a loop.
Last edited by Lord Orwell; Jun 23rd, 2009 at 02:34 AM.
-
Jun 24th, 2009, 06:21 PM
#17
Re: add all files from a folder to a listbox?
 Originally Posted by Lord Orwell
a couple of reasons.
1. There isn't any other solution for getting the location of the user's folder in question. Yes, there are default locations, but these locations aren't even in the same places in different operating systems. Your desktop location for example:
win95/98/me:
c:\windows\desktop (assuming they name their install windows)
2k/xp:
c:\documents and settings\users\username\desktop
vista\7:
c:\users\username\desktop
and these are merely default locations. They can be moved by the user to anywhere they wish, even other hard drives.
2. The filelist control is an activex control and if you use it instead of the standard listbox, it has to be included with the compiled program, making the distributable larger. This is especially apparent with the common dialog control. the control is a couple of megabytes in size, and about 20 lines of code can reproduce it.
3. Using the controls through api can give you more functionality than the control itself gives you through vb. Richedit comes to mind. It has link functionality that isn't used in the richtextbox control.
4. The code is actually smaller. If you strip down that code posted above, it's actually a declaration and a call and that's about it. None of the constants are required.
Code:
Cnt = SendMessage(List1.hwnd, &H18d, 7, ByVal "c:\*.*")
besides the single sendmessage declaration, that's all that is required to load the c:\ directory into the listbox, and it's much, MUCH faster than the dir$ command in a loop.
Lord Orwell, I appreciate your opinion. However, we could also write the same solution in Assembly language with about 300 lines of code. I am sure it would run even faster than the API you suggest.
I still lilke writing code in VB6 using 6 or 7 lines, even if I lose a few nanoseconds.
-
Jun 24th, 2009, 11:06 PM
#18
Re: add all files from a folder to a listbox?
don't get me wrong. Personally, i only hit api if i need it for something that's going to take a load of code to replace. I really don't see anything i posted above being an opinion though. You asked for legitimate reasons and i gave some.
How exactly do you intend to get the location of a user's folder WITHOUT api though? I'd love to see that code.
And personally i have written a recursive search in assembly, and it wasn't that large. about 20 lines. It didn't use a listbox though. It was in dos.
And much, MUCH faster does not equal a few nanoseconds. We're talking visible differences in speed. The refresh of the listbox alone makes the difference. the file access probably isn't any faster. It's the event triggering for every single item you add to the listbox. Of course you could add more lines to your code to counteract that, but then again your code will no longer be 6 or 7 lines. It's the problem with controls in general. Reading and writing to them is intrinsically slow, plus the event-triggering overhead. Just based on experience, i would expect a directory of signifigant size to have a speed difference of at least a magnitude of 100 times. ten milliseconds as compared to a second, in other words.
Of course, that's only if speed matters. Otherwise just use the filelist control and don't use any code at all.
Also (and this is a nitpick), it wasn't i who suggested it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|