Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2007-04-06
14
Medium Priority
?
285 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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
 
LVL 27

Accepted Solution

by:
VBRocks earned 1600 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 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 400 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 1600 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The question appears often enough, how do I transfer my data from my old server to the new server while preserving file shares, share permissions, and NTFS permisions.  Here are my tips for handling such a transfer.
The business world is becoming increasingly integrated with tech. It’s not just for a select few anymore — but what about if you have a small business? It may be easier than you think to integrate technology into your small business, and it’s likely…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This Micro Tutorial will teach you how to reformat your flash drive. Sometimes your flash drive may have issues carrying files so this will completely restore it to manufacturing settings. Make sure to backup all files before reformatting. This w…
Suggested Courses

688 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