Solved

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

Posted on 2007-04-06
14
272 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
ID: 18864119
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
ID: 18864130
Of course, the "c:\path" would be the combobox text:

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

Expert Comment

by:ia2189
ID: 18864156
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
ID: 18864162
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
ID: 18864186
vb6
0
 
LVL 27

Expert Comment

by:VBRocks
ID: 18864294
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
ID: 18864464
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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 27

Accepted Solution

by:
VBRocks earned 400 total points
ID: 18864510
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
ID: 18864571
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
ID: 18864583
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
ID: 18878357
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
ID: 18930976
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
ID: 18931349
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
ID: 18931746
Thanks for all the help
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Best web interface for online storage 3 83
Macro Excel - Multiple If conditions 2 64
Veeam Manuall Backup 2 56
What namespace do I need to import? 2 8
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
How to update Firmware and Bios in Dell Equalogic PS6000 Arrays and Hard Disks firmware update.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This tutorial will walk an individual through the process of installing the necessary services and then configuring a Windows Server 2012 system as an iSCSI target. To install the necessary roles, go to Server Manager, and select Add Roles and Featu…

920 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

18 Experts available now in Live!

Get 1:1 Help Now