Highlights
All Snippets
Top 100 Snippets
Librarians
gbCodeLib

By Language
VB6
JavaScript
Perl
HTML
SQL
Java
DOS

GBIC >> Source Code >> Visual Basic >> Snippet

Select folder (dialog window)


Private Sub  SelectFolder()
  Dim bi As BROWSEINFO
  Dim pidl As Long
  Dim path As String
  Dim pos As Long
   
'Fill the BROWSEINFO structure With the
'needed data. To accommodate comments, the
'With/End With syntax has not been used, though
'it should be your 'final' version.

   With bi
     
     'hwnd of the window that receives messages
     'from the Call. Can be your application
     'or the handle from GetDesktopWindow()
     .hOwner = Me .hwnd

     'pointer to the Item identifier list specifying
     'the location of the "root" folder to browse from.
     'if Null, the desktop folder Is used.
    .pidlRoot = 0&

     'message to be displayed In the Browse dialog
    .lpszTitle = "Select image folder"

     'the type of folder to return.
     .ulFlags = BIF_RETURNONLYFSDIRS
   End With
   
  'show the browse for folders dialog
  pidl = SHBrowseForFolder(bi)

  'the dialog has closed, so parse & display the
  'user's returned folder selection contained in pidl
  path = Space$(MAX_PATH)
   
   If SHGetPathFromIDList( ByVal pidl, ByVal path) Then
     pos = InStr(path, Chr $(0))
     txtFolder.Text = Lcase $(Left(path, pos - 1))
   End If

   Call CoTaskMemFree(pidl)
End Sub

'=========================================
'alternative source code


Public Function  BrowseForFolder(hwndOwner As Long , sPrompt As String ) As String
     
    'declare variables to be used
     Dim iNull As Integer
     Dim lpIDList As Long
     Dim lResult As Long
     Dim sPath As String
     Dim udtBI As BrowseInfo

    'initialise variables
     With udtBI
       .hwndOwner = hwndOwner
       .lpszTitle = lstrcat(sPrompt, "" )
       .ulFlags = BIF_RETURNONLYFSDIRS
     End With

    'Call the browse for folder API
    lpIDList = SHBrowseForFolder(udtBI)
     
    'get the resulting string path
     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

    'if cancel was pressed, sPath = ""
    BrowseForFolder = sPath

End Function