Click to See Complete Forum and Search --> : combobox


dafabe
May 22nd, 2001, 05:56 AM
As part of a program I am writing i need to be able to have a combobox fill with a list of files in a directory. so they can be selected and then processed.

can anyone please help?



-DAVE

shree
May 22nd, 2001, 07:00 AM
Use an invisible file list box. It will list for you all files in a directory. Then write a loop that will add all items of this file list box to the combo box.

Cimperiali
May 22nd, 2001, 07:10 AM
look at this:
'one command button, one combobox (named "cboCombo")
Option Explicit

'original code from Kamilche

Private Sub FileList(ByVal Pathname As String, Optional DirCount As Long, Optional FileCount As Long)
'at this directory level and lower.
'Example of usage:
'RichTextBox1.Text = FileList("c:\window
' s")
Dim ShortName As String, LongName As String
Dim NextDir As String
Static FolderList As Collection
Screen.MousePointer = vbHourglass
'First time through only, create collect
' ion
'to hold folders waiting to be processed
' .


If FolderList Is Nothing Then
Set FolderList = New Collection
FolderList.Add Pathname
DirCount = 0
FileCount = 0
End If


Do
'Obtain next directory from list
NextDir = FolderList.Item(1)
'Remove next directory from list
FolderList.Remove 1
'List files in directory
ShortName = Dir(NextDir & "\*.*", vbNormal Or _
vbArchive Or _
vbDirectory)


Do While ShortName > ""


If ShortName = "." Or ShortName = ".." Then
'skip it
Else
'process it
LongName = NextDir & "\" & ShortName


If (GetAttr(LongName) And vbDirectory) > 0 Then
'it's a directory - add it to the list o
' f directories to process
FolderList.Add LongName
DirCount = DirCount + 1
Else
'it's a file - add it to the list of fil
' es.
cboCombo.AddItem LongName

FileCount = FileCount + 1
End If
End If
ShortName = Dir()
Loop
Loop Until FolderList.Count = 0
Screen.MousePointer = vbNormal
End Sub
Private Sub Command1_Click()
Call FileList("c:")
End Sub


Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.