How to populate dropdown box with files from a local drive.

I am trying to create a program that will read a drive and populate a dropdown box with the files in that drive and allow the user to open the files in what ever program they run in. Any help would be appreciated. Maximum points awarded
LVL 9
samiam41Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

VBRocksCommented:
Can you clarify your question just a bit?  You have folders and files that can be displayed; do you only want files on a particular drive, like the "C:" drive?

You can do this on the Form load event:
        Me.ComboBox1.DataSource = My.Computer.FileSystem.GetDirectories("c:\", FileIO.SearchOption.SearchTopLevelOnly)

And then, if they select a file, you can use this line of code to start the program they select:
System.Diagnostics.Process.Start("c:\path")
0
VBRocksCommented:
Of course, the "c:\path" would be the combobox text:

System.Diagnostics.Process.Start(me.ComboBox1.Text)
0
ia2189Commented:
Are you wanting VB6 or VB.NET?  VB6 example is below.

Option Explicit
Dim fso As New FileSystemObject
   
Private Sub cboItems_Click()

   Dim oObject
   
   Open "C:\temp\temp.bat" For Output As #1
   Print #1, "C:\" & cboItems.Text
   Close #1

   oObject = Shell("C:\temp\temp.bat", vbNormalFocus)

End Sub

Private Sub Form_Load()

   Dim fso As New FileSystemObject
   Dim oFolder As Folder
   Dim oFile As File
   
   cboItems.Clear
   
   Set oFolder = fso.GetFolder("C:\")
   
   For Each oFile In oFolder.Files
   
      cboItems.AddItem oFile.Name
   
   Next

End Sub
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

samiam41Author Commented:
Sorry, let me clerify further. I want to populate a dropdown box with the folders on a perticular drive, from there the user will click on a folder and it will populater another dropdown box with folders from that folder and from there i want to populate a listbox with the files from that folder for the user to choose and open. The files will be .pdf so they will need to load adobe acrobat reader.
0
samiam41Author Commented:
vb6
0
VBRocksCommented:
Here is an approach that will be easy for you:

Private Sub Combo1_Change()
    loadCombo Me.Combo1.Text, Me.Combo2
   
End Sub

Private Sub Form_Load()
        loadCombo "C:\", Me.Combo1
       
End Sub

Private Sub loadCombo(ByVal folder As String, ByRef loadCombo As ComboBox)
    Dim fso, f, f1, fc
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.GetFolder(folder)
    Set fc = f.SubFolders
    For Each f1 In fc
        loadCombo.AddItem (f1.Name)
    Next

End Sub
0
VBRocksCommented:
One more thing.  Since this is VB6, instead of using a combobox, why don't you just use a DriveListBox and a FileListBox?  They are so easy, and this is the only code you need, unless you have multiple ones.

Private Sub Drive1_Change()
    Me.File1.Path = Me.Drive1.drive
   
End Sub
0
VBRocksCommented:
Sorry, one last thing.  This is the best solution:  3 controls:

DriveListBox
DirListBox
FileListBox

And this is all the code you need:
Private Sub Dir1_Change()
    Me.File1.Path = Me.Dir1.Path
   
End Sub

Private Sub Drive1_Change()
    Me.Dir1.Path = Me.Drive1.drive
   
End Sub

And it looks very professional to boot!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
VBRocksCommented:
Here's one last post.  Same as above, but includes your Shell command to open the program if it is a Text file.  With the Shell command, you can open a file of any program type, but you need to associate it with a program, or just enter the path for the Executible for the program, and then the path of the selected file as an argument.  I'll leave that with you though.  Good luck.

Private Sub Command1_Click()
    If InStr(1, Me.File1.FileName, "txt") > 0 Then
        Shell "Notepad " & Me.File1.Path & Me.File1.FileName, vbNormalNoFocus
   
    Else
        MsgBox "You have to configure the correct program to run this file."
       
    End If
   
End Sub

Private Sub Dir1_Change()
    Me.File1.Path = Me.Dir1.Path
   
End Sub

Private Sub Drive1_Change()
    Me.Dir1.Path = Me.Drive1.drive
   
End Sub
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here are the controls:
http://www.ster.be/vb/controls.gif
DriveListBox (grey rectangle) is in the middle on the third row from the bottom.
DirListBox (yellow folder) is to the right of the DriveListBox.
FileListBox (white sheet) is on the left on the second row from the bottom.

Here is a picture of the three controls working together:
http://ccrp.mvps.org/images/controls/ftvfiles.gif

You can set the Pattern of the FileListBox so that it only displays .pdf files.  Use the ShellExecute() API to open the pdfs when they are double clicked:

Option Explicit

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Private Sub Form_Load()
    File1.Pattern = "*.pdf"
End Sub

Private Sub Drive1_Change()
    Dir1.path = Drive1.Drive
End Sub

Private Sub Dir1_Change()
    File1.path = Dir1.path
End Sub

Private Sub File1_DblClick()
    Dim result As Long
    Dim path As String
    Dim fileName As String
   
    path = Dir1.path
    If Right(path, 1) <> "\" Then
        path = path & "\"
    End If
    fileName = File1.List(File1.ListIndex)
    result = ShellExecute(Me.hWnd, "open", path & fileName, vbNullString, "C:\", 1)
End Sub
0
samiam41Author Commented:
Thanks Idle_Mind and vbrocks but before I award points is there a way to open more then one file at a time.
0
samiam41Author Commented:
I need to be able to allow the user to select multiple files to open at once is this possible using the already suggested solutions.
0
VBRocksCommented:
Sure, just set the MultiSelect property to Simple, then you can retrieve the selected items as follows:

Dim i As Integer

For i = 0 To Me.File1.ListCount - 1
    If Me.File1.Selected(i) = True Then
        'process
       
        Debug.Print Me.File1.List(i)
       
    End If
Next i

0
samiam41Author Commented:
Thanks for all the help
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.