Results 1 to 3 of 3

Thread: [RESOLVED] Browse for folders?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    278

    Resolved [RESOLVED] Browse for folders?

    I need some help!

    To browse for folders I'm using this code:
    Code:
    Dim sh As New Shell32.Shell
    Text1.Text = sh.BrowseForFolder(0, "Select a Folder", 0, 0)
    Almost everything is fine but there is one problem:

    Then I'm selecting an folder I'm geting not full directory address in TextBox but just folder name. I need full directory addres not just folder name.

    So mybe someone can help me to solve this problem ?
    Last edited by Lauriux1; Sep 27th, 2007 at 08:09 AM.

  2. #2
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: Browse for folders?

    This is something I used for browsing folders

    http://vbnet.mvps.org/index.html?cod...wsefolders.htm




    EDIT: removed the my version of the code and put a link to original source.
    Last edited by zeezee; Mar 23rd, 2008 at 05:14 AM. Reason: Put Link to original code
    IIF(Post.Rate > 0 , , )

  3. #3
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Browse for folders?

    Try this:
    VB Code:
    1. 'In a form
    2. MyDir = BrowseFolder(InitDir)
    3. 'where InitDir is the directory you want the browser
    4. 'to be pointing at when the function is called
    5. '--------------
    6. 'In a module:
    7. Option Explicit
    8. Const MAX_PATH = 260
    9. Const BIF_RETURNONLYFSDIRS = &H1&
    10. Const BIF_STATUSTEXT = &H4
    11. Const WM_USER = &H400
    12. Const BFFM_INITIALIZED = 1
    13. Const BFFM_SELCHANGED = 2
    14. Const BFFM_SETSTATUSTEXT = WM_USER + 100
    15. Const BFFM_SETSELECTION = WM_USER + 102
    16. Const SHGFI_DISPLAYNAME = &H200
    17. Const SHGFI_PIDL = &H8
    18. Const WM_SETTEXT = &HC
    19. Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    20. Declare Function BrowseForFolder Lib "shell32" Alias "SHBrowseForFolder" (lpbi As BROWSEINFO) As Long
    21. Declare Function GetPathFromIDList Lib "shell32" Alias "SHGetPathFromIDList" (ByVal pidl As Long, ByVal pszPath As String) As Long
    22. Declare Function SHGetFileInfo Lib "shell32" Alias "SHGetFileInfoA" (ByVal pszPath As Any, ByVal dwFileAttributes As Long, psfi As SHFILEINFO, ByVal cbFileInfo As Long, ByVal uFlags As Long) As Long
    23. Declare Function SendMessage Lib "USER32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    24. Type BROWSEINFO
    25.     hwndOwner       As Long
    26.     pidlRoot        As Long
    27.     pszDisplayName  As String
    28.     lpszTitle       As String
    29.     ulFlags         As Long
    30.     lpfn            As Long
    31.     lParam          As String
    32.     iImage          As Long
    33. End Type
    34. Type SHFILEINFO
    35.     hIcon           As Long
    36.     iIcon           As Long
    37.     dwAttributes    As Long
    38.     szDisplayName   As String * MAX_PATH
    39.     szTypeName      As String * 80
    40. End Type
    41.  
    42. Public Function BrowseFolder(StartFolder As String) As String
    43.  
    44.     Dim bi As BROWSEINFO, pidl As Long, Path As String * MAX_PATH
    45.     CopyMemory bi.lpfn, AddressOf BrowseCallbackProc, 4
    46.     bi.ulFlags = BIF_RETURNONLYFSDIRS Or BIF_STATUSTEXT
    47.     bi.hwndOwner = Screen.ActiveForm.hWnd
    48.     bi.lParam = StrConv(Trim$(StartFolder), vbUnicode)
    49.     bi.lpszTitle = "Select a folder from the tree."
    50.     pidl = BrowseForFolder(bi)
    51.     If pidl Then
    52.         GetPathFromIDList pidl, Path
    53.         BrowseFolder = Left$(Path, InStr(Path, vbNullChar) - 1)
    54.     Else
    55.         BrowseFolder = StartFolder
    56.     End If
    57. End Function
    58.  
    59. Function BrowseCallbackProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal lParam As Long, ByVal lpData As String) As Long
    60.  
    61.     Dim Path As String * MAX_PATH
    62.     If uMsg = BFFM_SELCHANGED Then
    63.         GetPathFromIDList lParam, Path
    64.         If Asc(Path) = 0 Then
    65.             Dim sfi As SHFILEINFO
    66.             SHGetFileInfo lParam, 0, sfi, Len(sfi), SHGFI_DISPLAYNAME Or SHGFI_PIDL
    67.             Path = sfi.szDisplayName
    68.         End If
    69.         'Uncomment the following line if the current directory
    70.         'path is to be displayed on top of the treeview
    71.         'SendMessage hWnd, BFFM_SETSTATUSTEXT, 0&, ByVal Path
    72.     ElseIf uMsg = BFFM_INITIALIZED Then
    73.         SendMessage hWnd, BFFM_SETSELECTION, True, ByVal lpData
    74.         SendMessage hWnd, WM_SETTEXT, 0, ByVal "Browse for Folder"
    75.     End If
    76. End Function
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

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