Solved

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

Posted on 2007-04-06
14
270 Views
Last Modified: 2013-11-25
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
0
Comment
Question by:samiam41
14 Comments
 
LVL 27

Expert Comment

by:VBRocks
Comment Utility
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
 
LVL 27

Expert Comment

by:VBRocks
Comment Utility
Of course, the "c:\path" would be the combobox text:

System.Diagnostics.Process.Start(me.ComboBox1.Text)
0
 
LVL 3

Expert Comment

by:ia2189
Comment Utility
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
 
LVL 9

Author Comment

by:samiam41
Comment Utility
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
 
LVL 9

Author Comment

by:samiam41
Comment Utility
vb6
0
 
LVL 27

Expert Comment

by:VBRocks
Comment Utility
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
 
LVL 27

Expert Comment

by:VBRocks
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 27

Accepted Solution

by:
VBRocks earned 400 total points
Comment Utility
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
 
LVL 27

Expert Comment

by:VBRocks
Comment Utility
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
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 100 total points
Comment Utility
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
 
LVL 9

Author Comment

by:samiam41
Comment Utility
Thanks Idle_Mind and vbrocks but before I award points is there a way to open more then one file at a time.
0
 
LVL 9

Author Comment

by:samiam41
Comment Utility
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
 
LVL 27

Assisted Solution

by:VBRocks
VBRocks earned 400 total points
Comment Utility
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
 
LVL 9

Author Comment

by:samiam41
Comment Utility
Thanks for all the help
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Files ready to be written to disk? 10 57
Basics of NetApp 10 55
vb6 connector to mongodb 2 23
Free NAS Virtual Appliance 4 52
Lets start to have a small explanation what is VAAI(vStorage API for Array Integration ) and what are the benefits using it. VAAI is an API framework in VMware that enable some Storage tasks. It first presented in ESXi 4.1, but only after 5.x sup…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now