• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1719
  • Last Modified:

How to show a Directory Tree in VB6?

How do I show a directory tree dialog screen so that the user can choose a file?

0
rrr333222
Asked:
rrr333222
  • 5
  • 5
  • 3
  • +2
1 Solution
 
caraf_gCommented:
You're contradicting yourself. Do you want the user to choose a folder or a file?
0
 
Bob LearnedCommented:
To browse for a file you can use a common dialog box.

To browse for a folder you could use:

Here's some easy to use code for displaying a directory browsing
window. This allows a user to select a directory.

Private Type BrowseInfo
  hWndOwner      As Long
  pIDLRoot       As Long
  pszDisplayName As Long
  lpszTitle      As Long
  ulFlags        As Long
  lpfnCallback   As Long
  lParam         As Long
  iImage         As Long
End Type

'Browsing for directory.
Private Const BIF_RETURNONLYFSDIRS = &H1      'For finding a folder to
start document searching
Private Const BIF_DONTGOBELOWDOMAIN = &H2     'For starting the Find
Computer
Private Const BIF_STATUSTEXT = &H4
Private Const BIF_RETURNFSANCESTORS = &H8

Private Const BIF_BROWSEFORCOMPUTER = &H1000  'Browsing for Computers.
Private Const BIF_BROWSEFORPRINTER = &H2000   'Browsing for Printers
Private Const BIF_BROWSEINCLUDEFILES = &H4000 'Browsing for Everything

Private Const MAX_PATH = 260

Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal _
lpString1 As String, ByVal lpString2 As String) As Long

Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As _
BrowseInfo) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList
As _
Long, ByVal lpBuffer As String) As Long

Public Function BrowseForFolder(hWndOwner As Long, sPrompt As String)
As String

   '=================================================
   'Opens the system dialog for browsing for a folder
   '=================================================
   Dim iNull As Integer
   Dim lpIDList As Long
   Dim lResult As Long
   Dim sPath As String
   Dim udtBI As BrowseInfo

  With udtBI
     .hWndOwner = hWndOwner
     .lpszTitle = lstrcat(sPrompt, "")

     .ulFlags = BIF_RETURNONLYFSDIRS
  End With

  lpIDList = SHBrowseForFolder(udtBI)
  If lpIDList Then
     sPath = String$(MAX_PATH, 0)
     lResult = SHGetPathFromIDList(lpIDList, sPath)
     Call CoTaskMemFree(lpIDList)
     iNull = InStr(sPath, vbNullChar)
     If iNull Then
        sPath = Left$(sPath, iNull - 1)
     End If
  End If

  BrowseForFolder = sPath

End Function
0
 
Erick37Commented:
Use the ShowOpen method of the CommonDialog control.

From VB help:

Private Sub Command1_Click()
    ' Set CancelError is True
    CommonDialog1.CancelError = True
    On Error GoTo ErrHandler
    ' Set flags
    CommonDialog1.Flags = cdlOFNHideReadOnly
    ' Set filters
    CommonDialog1.Filter = "All Files (*.*)|*.*|Text Files" & _
    "(*.txt)|*.txt|Batch Files (*.bat)|*.bat"
    ' Specify default filter
    CommonDialog1.FilterIndex = 2
    ' Display the Open dialog box
    CommonDialog1.ShowOpen
    ' Display name of selected file
    MsgBox CommonDialog1.filename
    Exit Sub
   
ErrHandler:
    'User pressed the Cancel button
    Exit Sub
End Sub
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
JuiletteCommented:

Thelearneon
you can select a directoy using fso as well...don't have the code but it's in help.
0
 
JuiletteCommented:
The browser(win-explorer type) control works as well.
0
 
caraf_gCommented:
What's fso?
0
 
rrr333222Author Commented:
TheLearnedOne
It's a folder that I want to select. I tried the code you provided but nothing is happening. I am assuming that I should be using/calling the BrowseForFolder() function, but does it make a diference what the parameteres are?
0
 
rrr333222Author Commented:
I check FSO but did not find a way to use it to get a Tree of the drive/paths, etc.
0
 
Bob LearnedCommented:
You can call the BrowseForFolder routine like this:

FileName = BrowseForFolder(Form1.hWnd, "Select Folder")
0
 
caraf_gCommented:
Check out www.mvps.org/ccrp

you'll want the ccrpbds6.dll It's beautiful.
0
 
caraf_gCommented:
Again, what is FSO?
0
 
Bob LearnedCommented:
FSO is the FileSystemObject in the Scripting library
0
 
caraf_gCommented:
Aha. Thank you Learned One.
0
 
rrr333222Author Commented:
I am trying to browse for a Folder, sorry if I was not clear in the question. I wouldn't mind giving you the points as well, but since I can only give to one person I have to give it to theLearnedOne
0
 
rrr333222Author Commented:
I am trying to browse for a Folder, sorry if I was not clear in the question. I wouldn't mind giving you the points as well, but since I can only give to one person I have to give it to theLearnedOne
0
 
rrr333222Author Commented:
i increased the points, as i see that this was more involved than  i expected it to be.
thanks.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 5
  • 5
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now