free web hosting | free website | Web Hosting | Free Website Submission | shopping cart | Promoter Online | php hosting
affordable web hosting Pets web page hosting web hosting website hosting web hosting service web hosting web host
 

Main

Aplications

Games

User Controls

Class

Code Snipets

Tips

Add-Ins

Other stuff

Contact

Gest Book

About

BrowseForFolder

If we need allow the user select a folder the Microsoft Common Dialog Control is not
going to be very helpful, it should have a browse for folder dialog, but it hasn't.
But there's a API function that provides that dialog box. If you want more stuff about
Windows API go to www.allapi.net.
Lets get to work, here is the class code:


Option Explicit

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
Const BIF_RETURNONLYFSDIRS = 1
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 FolderName As String ' this is the property that will indicate the
' selected folder


Public Function Browse(ByVal Owner As Long, ByVal Title As String) As String

    Dim iNull As Integer, lpIDList As Long, lResult As Long
    Dim sPath As String, udtBI As BrowseInfo

    With udtBI
        'Set the owner window
        .hWndOwner = Owner
        'lstrcat appends the two strings and returns the memory address
        .lpszTitle = lstrcat(Title, "")
        'Return only if the user selected a directory
        .ulFlags = BIF_RETURNONLYFSDIRS
    End With

    'Show the 'Browse for folder' dialog
    lpIDList = SHBrowseForFolder(udtBI)
    If lpIDList Then
        sPath = String$(MAX_PATH, 0)
        'Get the path from the IDList
        SHGetPathFromIDList lpIDList, sPath
        'free the block of memory
        CoTaskMemFree lpIDList
        iNull = InStr(sPath, vbNullChar)
        If iNull Then
            sPath = Left$(sPath, iNull - 1)
        End If
    End If

    FolderName = sPath
    Browse = sPath

End Function
 


We can call the Browse function to display the Dialog box, the Owner parameter is hWnd property of the form calling
the dialog, the Title parameter is the message displayed in the dialog box.
The user selected folder can be retrieved by the output of the Browse function or by the FolderName property.
 

Links:

Page Last Updated: 02-07-2003