Results 1 to 6 of 6

Thread: Folder select

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2001
    Location
    Viet Nam
    Posts
    98

    Folder select

    Can I use code to browse a dialog to select folder?

  2. #2
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    From Matt Gates

    In a module:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Const MAX_PATH = 260
    4.  
    5. Public Enum browseInfoFlags
    6.   BIF_NONE = 0 ' No Flags
    7.   BIF_RETURNONLYFSDIRS = &H1&      ' For finding a folder to start document searching
    8.   BIF_DONTGOBELOWDOMAIN = &H2&     ' For starting the Find Computer
    9.   BIF_STATUSTEXT = &H4&
    10.   BIF_RETURNFSANCESTORS = &H8&
    11.   BIF_EDITBOX = &H10&
    12.   BIF_VALIDATE = &H20&              ' insist on valid result (or CANCEL)
    13.  
    14.   BIF_BROWSEFORCOMPUTER = &H1000&  ' Browsing for Computers.
    15.   BIF_BROWSEFORPRINTER = &H2000&   ' Browsing for Printers
    16.   BIF_BROWSEINCLUDEFILES = &H4000& ' Browsing for Everything
    17. End Enum
    18.  
    19. Private Type BROWSEINFO
    20.    hWndOwner As Long
    21.    pidlRoot As Long
    22.    pszDisplayName As String
    23.    lpszTitle As String
    24.    ulFlags As browseInfoFlags
    25.    lpfn As Long
    26.    lParam As Long
    27.    iImage As Long
    28. End Type
    29.  
    30. ' for SHGetPathFromIDList, pszString must be at least MAX_PATH (260) chars
    31.  
    32. Private Declare Function SHBrowseForFolder Lib "shell32" Alias "SHBrowseForFolderA" (lpbi As BROWSEINFO) As Long
    33. Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidl As Long, ByVal pszPath As String) As Long
    34. Private Declare Sub CoTaskMemFree Lib "ole32" (ByVal ptr As Long)
    35.  
    36. ' BrowseForFolder() returns a path if the user picks a good one, or "" if they don't.
    37. Public Function BrowseForFolder(ByVal hWndOwner As Long, Optional ByVal dlgTitle$ = "Please select a folder.", Optional ByVal flags As browseInfoFlags = BIF_RETURNONLYFSDIRS) As String
    38.   Dim bif As BROWSEINFO, pidl As Long, buf$
    39.  
    40.   With bif
    41.     .hWndOwner = hWndOwner
    42.     .pidlRoot = 0          ' desktawp
    43.     .pszDisplayName = Space$(MAX_PATH)
    44.     .lpszTitle = dlgTitle$
    45.     .ulFlags = flags
    46.     .lpfn = 0&
    47.     .lParam = 0&
    48.     .iImage = 0&
    49.   End With
    50.   pidl = SHBrowseForFolder(bif)
    51.   If (pidl = 0) Then Exit Function
    52.   buf$ = Space$(MAX_PATH)
    53.   If (SHGetPathFromIDList(pidl, buf$) = 0) Then
    54.     buf$ = ""
    55.   Else
    56.     buf$ = Left$(buf$, InStr(1, buf$, vbNullChar) - 1)
    57.   End If
    58.   CoTaskMemFree pidl
    59.   BrowseForFolder = buf$
    60. End Function



    In a form:
    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim strResFolder As String
    3.     strResFolder = BrowseForFolder(hWnd, "Please select a folder.")
    4.     If strResFolder = "" Then
    5.         Call MsgBox("The Cancel button was pressed.", vbExclamation)
    6.     Else
    7.         Call MsgBox("The folder " & strResFolder & " was selected.", vbExclamation)
    8.     End If
    9. End Sub

  3. #3
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Change the InitDir property

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  4. #4
    Matthew Gates
    Guest
    VB Code:
    1. Option Explicit
    2.  
    3. Private Type BROWSEINFO
    4.     hwndOwner As Long
    5.     pIDLRoot As Long
    6.     pszDisplayName As Long
    7.     lpszTitle As Long
    8.     ulFlags As Long
    9.     lpfnCallback As Long
    10.     lParam As Long
    11.     iImage As Long
    12. End Type
    13.  
    14. Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
    15. Private Declare Function lStrCat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
    16. Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BROWSEINFO) As Long
    17. Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long
    18.  
    19. Private Const BIF_RETURNONLYFSDIRS = 1
    20.  
    21. Public Function BrowseForFolder(ByVal lHwnd As Long, ByVal sPrompt As String) As String
    22.     Dim tBI As BROWSEINFO
    23.     Dim lList As Long
    24.     Dim lResult As Long
    25.     Dim sPath As String
    26.     Dim sString As String
    27.    
    28.     sString = Space(260)
    29.     With tBI
    30.         .hwndOwner = lHwnd
    31.         .lpszTitle = lStrCat(sPrompt, Chr(0))
    32.         .pszDisplayName = StrPtr(sString)
    33.         .ulFlags = BIF_RETURNONLYFSDIRS
    34.     End With
    35.     lList = SHBrowseForFolder(tBI)
    36.     sString = StrConv(sString, vbUnicode)
    37.     If lList Then
    38.         sPath = Space(260)
    39.         lResult = SHGetPathFromIDList(lList, sPath)
    40.         Call CoTaskMemFree(lList)
    41.         sPath = Left$(sPath, InStr(sPath, Chr(0)) - 1)
    42.     End If
    43.     BrowseForFolder = sPath
    44. End Function
    45.  
    46.  
    47.  
    48. Private Sub Command1_Click()
    49.     Msgbox BrowseForFolder(hWnd, "Select Directory..")
    50. End Sub

  5. #5
    Matthew Gates
    Guest
    Maybe I should retire now..I'm getting to old and slow for this .

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jun 2001
    Location
    Viet Nam
    Posts
    98
    Thanks for the code!
    And now I want to set default folder for the dialog.
    What i have to do?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width