ShowFolders Method

Purpose

Creates a dialog box that allows the user to select a folder.

Syntax

CommDlg.FileName [= string]

CommDlg.ShowFolders[(flag)]

CommDlg.Title [= string]

Description

Displays a dialog box that enables the user to select a shell folder. The optional flag (Variant) specifies the style for the dialog box.

The Title property can be used to set a customized dialog box title, while the FileName property serves both as a receptable for the default folder beforehand and the file path once a folder has been chosen.

Example

$Library "gfawinx"              // for the BIF_* constants

Ocx CommDlg cd

cd.Title = "Select directory"

cd.FileName = "c:\windows"      ' sets the default folder

cd.ShowFolders BIF_USENEWUI

Print cd.FileName

Remarks

The following constants are added through the Gfawinx.lg32 library:

Public Const BIF_RETURNONLYFSDIRS = $1

Public Const BIF_DONTGOBELOWDOMAIN = $2

Public Const BIF_STATUSTEXT = $4

Public Const BIF_RETURNFSANCESTORS = $8

Public Const BIF_EDITBOX  0x0010

Public Const BIF_VALIDATE 0x0020   // insist on valid result (or CANCEL)

Public Const BIF_NEWDIALOGSTYLE = 0x0040   // Use the new dialog layout with the ability to resize.

Public Const BIF_USENEWUI = (BIF_NEWDIALOGSTYLE | BIF_EDITBOX)

Public Const BIF_NONEWFOLDERBUTTON = 0x0200   // Do not add the "New Folder" button to the dialog.  Only applicable with BIF_NEWDIALOGSTYLE."

Public Const BIF_BROWSEFORCOMPUTER = $1000

Public Const BIF_BROWSEFORPRINTER = $2000

Public Const BIF_BROWSEINCLUDEFILES = 0x4000  // Browsing for Everything

Known Issues

Noted recently is what could be described as the 'renamed folder' error: if you rename a folder in the Commdlg ShowFolders window and click 'OK' before finishing the edit (before pressing Enter or clicking on another folder), the folder will be renamed, but the value returned in cd.FileName will be that of the name of the folder before it was renamed, which will cause an error if you then try to access it; this happens even if you use the BIF_VALIDATE flag. This is not truly a GFA bug, but something to be aware of.
[Reported by James Gaite, 01/03/2017]


This Ocx object has had a very on/off performance with it working well sometimes and not at all at other times. Tested on Win8.1 using GFA IDE build 1169 with OCX build 1185, the above example works - this has not always been the case.

If you run into problems with ShowFolders, there is an available workaround:

Declare Function SHGetPathFromIDList Lib "shell32.dll" (ByVal pidl As Long, _

ByVal pszBuffer As String) As Long

Declare Function SHBrowseForFolder Lib "shell32.dll" (lpBrowseInfo As _

BROWSEINFO) As Long

Type BROWSEINFO

hOwner           As Long

pidlRoot         As Long

pszDisplayName   As Long

lpszTitle        As Long

ulFlags          As Long

lpfn             As Long

lParam           As Long

iImage           As Long

EndType

Const BIF_RETURNONLYFSDIRS As Long = &H1

Const BIF_DONTGOBELOWDOMAIN As Long = &H2

Const BIF_RETURNFSANCESTORS As Long = &H8

Const BIF_EDITBOX  &H10

Const BIF_VALIDATE &H20

Const BIF_NEWDIALOGSTYLE = &H40

Const BIF_USENEWUI = (BIF_NEWDIALOGSTYLE | BIF_EDITBOX)

Const BIF_NONEWFOLDERBUTTON = &H200

Const BIF_BROWSEFORCOMPUTER As Long = &H1000

Const BIF_BROWSEFORPRINTER As Long = &H2000

Const BIF_BROWSEINCLUDEFILES As Long = &H4000

Const BFFM_SETSELECTION = (WM_USER + 102)

Dim foldir$ = App.Path & "\" : Print foldir$

OpenW 1

Print BrowseForFolder(Win_1.hWnd, "Title") : Print foldir$

Print BrowseForFolder(Win_1.hWnd, "Title") : Print foldir$

 

Function BrowseForFolder(hnd%, Title As String, Optional Flags%) As String

Local bi As BROWSEINFO

Local Int pidl

Local path$ = Space$(512) + #0, buf$ = Space$(512) + #0

If Flags <= 0 Then Flags = BIF_RETURNONLYFSDIRS | BIF_USENEWUI

Title = Title + #0

bi.hOwner = hnd

bi.pidlRoot = 0

bi.lpszTitle = V:Title

buf = Space$(512) + #0

bi.pszDisplayName = V:buf

bi.ulFlags = Flags

bi.lpfn = ProcAddr(BrowseCallbackProc)

pidl = SHBrowseForFolder(bi)

If pidl

If SHGetPathFromIDList(pidl, path)

path = ZTrim$(path)

If Right$(path, 1) <> "\" Then path = path + "\"

foldir$ = path$

Else

path = "Error"

EndIf

Else

path = ""

EndIf

BrowseForFolder = path

~CoTaskMemFree(pidl)

EndFunc

 

Function BrowseCallbackProc(hwnd As Handle, uMsg As Int, lp%, pData%) As Int

Switch(uMsg)

Case 1                   'BFFM_INITIALIZED :

~SendMessage(hwnd, BFFM_SETSELECTION, True, V:foldir$)

Case 2

Print "This is option 2"

EndSelect

Return 0

EndFunction

See Also

CommDlg

{Created by Sjouke Hamstra; Last updated: 24/08/2021 by James Gaite}