Click to See Complete Forum and Search --> : CommonDialog


ramirorivera
August 24th, 2001, 11:38 AM
How can I get a control with the same functionality as The CommonDialog but for locating folders instead of files. Basically I dont want the "open file" text box in the control beacause I'm just interested in the folder's path.

Thanks

John G Duffy
August 24th, 2001, 12:47 PM
You can use either the DirListBox control or the following sample that uses a faked out version of the CommonDialog control.
Start a new project. Add a Module. add A command button (cdExit) and a commonDialog control(cdMain)to the form.

'
' Past this code into the module
'
'***************************************************************
' Name: Using Common Dialog to Select Directories Instead
' Description:I had an occassion to want to use the common dialog
' box as a directory-picker instead of creating a separate form and
' placing a directory control on it.
' By: Tom Kumpf
'
'
' Inputs:None
'
' Returns:None
'
'Assumes:None
'
'Side Effects:None
'
'Code provided by Planet Source Code(tm) (http://www.PlanetSource
' Code.com) 'as is', without warranties as to performance, fitness,
' merchantability,and any other warranty (whether expressed or impl
' ied).
'***************************************************************



public Function GetFolders(cdmain)

'-- Cheap way to use the common dialog box as a directory-picker
Dim x as Integer
With cdmain
.Flags = cdlOFNPathMustExist
.Flags = .Flags Or cdlOFNHideReadOnly
.Flags = .Flags Or cdlOFNNoChangeDir
.Flags = .Flags Or cdlOFNExplorer
.Flags = .Flags Or cdlOFNNoValidate
.FileName = "*.*"
End With
'...The NoValidate setting permits the user to press "Open" while no
'single file is selected. The filename setting of "*.*" now satisfies
'the common dialog. to parse the directory, use the following logic
'from where you present the dialog:
x = 3
cdmain.CancelError = true 'do not terminate on error
on error resume next 'I will hande errors
cdmain.ShowOpen 'Present "open" dialog
'-- If FileTitle is null, user did not override the default (*.*)
'
If cdmain.FileTitle <> "" then x = len(cdmain.FileTitle)


If Err = 0 then
GetFolders = Left(cdmain.FileName, len(cdmain.FileName) - x)
else
'-- User pressed "Cancel"
GetFolders = ""
End If

End Function
'
' Paste this code into the Form
'
option Explicit


private Sub cmdExit_Click()
Unload me
End Sub

private Sub Form_Load()
'set the common dialog control (called "cdMain") using:
'-- Initialize Common Dialog control
Dim x
cdmain.InitDir = "C:\"
cdmain.DialogTitle = "Select a folder to process. Double click OPEN to Process"
x = GetFolders(cdmain)
Form1.Caption = x

End Sub






John G

Iouri
August 24th, 2001, 12:59 PM
'Module
Public Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type

Public Const BIF_RETURNONLYFSDIRS = &H1
Public Const BIF_DONTGOBELOWDOMAIN = &H2
Public Const BIF_STATUSTEXT = &H4
Public Const BIF_RETURNFSANCESTORS = &H8
Public Const BIF_BROWSEFORCOMPUTER = &H1000
Public Const BIF_BROWSEFORPRINTER = &H2000
Public Const MAX_PATH = 260

Public Declare Function SHGetPathFromIDList _
Lib "shell32.dll" Alias "SHGetPathFromIDListA" _
(ByVal pidl As Long, ByVal pszPath As String) As Long

Public Declare Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" _
(lpBrowseInfo As BROWSEINFO) As Long

Public Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal pv As Long)


'form (add2 buttons and label)

Private Sub cmdSelect_Click()

Dim bi As BROWSEINFO
Dim pidl As Long
Dim path As String
Dim pos As Integer

lblSelected = ""

'Fill the BROWSEINFO structure with the
'needed data. To accomodate comments, the
'With/End With sytax has not been used, though
'it should be your 'final' version.

'hwnd of the window that receives messages
'from the call. Can be your application
'or the handle from GetDesktopWindow().
bi.hOwner = Me.hWnd

'Pointer to the item identifier list specifying
'the location of the "root" folder to browse from.
'If NULL, the desktop folder is used.
bi.pidlRoot = 0&

'message to be displayed in the Browse dialog
bi.lpszTitle = "Select your Windows\System\ directory"

'the type of folder to return.
bi.ulFlags = BIF_RETURNONLYFSDIRS

'show the browse for folders dialog
pidl = SHBrowseForFolder(bi)

'the dialog has closed, so parse & display the
'user's returned folder selection contained in pidl
path = Space$(MAX_PATH)

If SHGetPathFromIDList(ByVal pidl, ByVal path) Then
pos = InStr(path, Chr$(0))
lblSelected = Left(path, pos - 1)
End If

Call CoTaskMemFree(pidl)

End Sub


Private Sub cmdEnd_Click()

Unload Me

End Sub






Iouri Boutchkine
iouri@hotsheet.com

ramirorivera
August 25th, 2001, 01:09 AM
Thank you very much for your help. This was exactily what I needed.

Ramiro