Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Common Dialog to retrieve folder names. 1

Status
Not open for further replies.

Brettsbignose1

Programmer
Jul 2, 2003
4
0
0
US
How would I use the Common Dialog control to retrieve folder names from the user. Without displaying other files.
If this is not the way to do it, I'm open to suggestions.
Brett
 
Hi,

If you want to use the Windows folder browser dialogue box, try this code:

Private Const BIF_STATUSTEXT = &H4&
Private Const BIF_RETURNONLYFSDIRS = 1
Private Const BIF_DONTGOBELOWDOMAIN = 2
Private Const BIF_USENEWUI = &H40
Private Const MAX_PATH = 260

Private Const WM_USER = &H400
Private Const BFFM_INITIALIZED = 1
Private Const BFFM_SELCHANGED = 2
Private Const BFFM_SETSTATUSTEXT = (WM_USER + 100)
Private Const BFFM_SETSELECTION = (WM_USER + 102)

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam 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
Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long

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

Private m_CurrentDirectory As String

Public Function WinBrowseForFolder(owner As Form, Title As String, StartDir As String) As String

Dim lpIDList As Long
Dim szTitle As String
Dim sBuffer As String
Dim tBrowseInfo As BrowseInfo
m_CurrentDirectory = StartDir & vbNullChar

szTitle = Title
With tBrowseInfo
.hWndOwner = owner.hwnd
.lpszTitle = lstrcat(szTitle, "")
.ulFlags = BIF_RETURNONLYFSDIRS + BIF_DONTGOBELOWDOMAIN + BIF_STATUSTEXT + BIF_USENEWUI
.lpfnCallback = GetAddressofFunction(AddressOf BrowseCallbackProc) 'get address of function.
End With

lpIDList = SHBrowseForFolder(tBrowseInfo)
If (lpIDList) Then
sBuffer = Space(MAX_PATH)
SHGetPathFromIDList lpIDList, sBuffer
sBuffer = Left(sBuffer, InStr(sBuffer, vbNullChar) - 1)
WinBrowseForFolder = sBuffer
Else
WinBrowseForFolder = ""
End If

End Function



- Andy
_______________________________
"On a clear disk you can seek forever"
 
I get "Variable not defined" on following line:

.lpfnCallback = GetAddressofFunction(AddressOf BrowseCallbackProc)

Who, what or where is BrowseCallbackProc ?


Andy
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
"Why does my program keep showing error messages every time something goes wrong?"
 
Thank you, Hypetia!


Andy
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
"Why does my program keep showing error messages every time something goes wrong?"
 


foldername = Replace(CommonDialog1.FileName, CommonDialog1.FileTitle, "")

David Paulson

 
Sadly not as useful as it might appear, dpaulson, as IMHO it is counterintuitive to have to select a file to get a folder.

In addition to Hypetia's solution I posted a slightly shorter version in thread222-573084. It has the advantage of being able to accept any of the Windows Special Folders as a root folder (eg ssfCOMMONSTARTMENU or ssfDESKTOP), but the disadvantage that you cannot browse above any root folder that you give it.
 
I agree with you strongm , but it does answer his original question.

David Paulson

 
I guess it's just a question of interpretation. I read it as "Is it possible just to list and select folders using the Commonn Dialog and, if not, what is an alternative?"
 
Hey,
I found it here:

Private Const BIF_RETURNONLYFSDIRS = 1
Private Const BIF_DONTGOBELOWDOMAIN = 2
Private Const MAX_PATH = 260

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

Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" _
(ByVal lpString1 As String, ByVal _
lpString2 As String) As Long

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


Private Function ListDir() As String
Dim lpIDList As Long
Dim sBuffer As String
Dim szTitle As String
Dim tBrowseInfo As BrowseInfo

szTitle = "Cookie Directory"
With tBrowseInfo
.hWndOwner = Me.hWnd
.lpszTitle = lstrcat(szTitle, "")
.ulFlags = BIF_RETURNONLYFSDIRS + BIF_DONTGOBELOWDOMAIN
End With

lpIDList = SHBrowseForFolder(tBrowseInfo)

If (lpIDList) Then
sBuffer = Space(MAX_PATH)
SHGetPathFromIDList lpIDList, sBuffer
sBuffer = Left(sBuffer, InStr(sBuffer, vbNullChar) - 1)
ListDir = sBuffer
End If

End Function


Thanks,
Brett
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top