devxlogo

Creating a “Browse for Folder” Dialog Box

Creating a “Browse for Folder” Dialog Box

Question:
How can I create a “browse” facility in my application&#151something which throws up a dialog box that allows the user to view folders and select one, as in Microsoft Explorer, without selecting a specific file within it?

Answer:
This is not a built-in feature of PowerBuilder; it requires the use of the Windows API. To begin with, create a new non-visual object and create the following local structures:

 structure shitemid unsignedint        cbcharacter        abidstructure itemidlistshitemid        mkidstructure browseinfounsignedlong        hownerunsignedlong        pidlrootstring        pszdisplaynamestring        lpsztitleunsignedint        ulflagsunsignedlong        lpfnlong        lparaminteger        iimage

Add the following API calls to the local external functions:

 Function unsignedlong &   SHGetPathFromIDListA( unsignedlong pidl, ref string pszPath) Library 'shell32'Function unsignedlong &   SHBrowseForFolderA( browseinfo lpbrowseinfo ) Library 'shell32'Subroutine CoTaskMemFree( ulong idlist ) Library 'ole32'

Create a function with the following arguments and return value:

 string browseforfolder (window awi_parent, string as_caption)

In this function add the following code:

 browseinfo lstr_biitemidlist lstr_idlunsignedlong ll_pidlunsignedlong ll_rInteger li_posString ls_Pathunsignedlong ll_NullSetNull( ll_Null )lstr_bi.hOwner = Handle( awi_Parent )lstr_bi.pidlRoot = 0lstr_bi.lpszTitle = as_captionlstr_bi.ulFlags = 1lstr_bi.pszDisplayName = Space( 255 )lstr_bi.lpfn = ll_Nullll_pidl = SHBrowseForFolderA( lstr_bi )ls_Path = Space( 255 )ll_R = SHGetPathFromIDListA( ll_pidl, ls_Path )CoTaskMemFree( ll_pidl )RETURN ls_Path

Save the object and in your script, create the object call the browseforfolder function, and then destroy the object.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist