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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Path Select in VB6 1

Status
Not open for further replies.

KVink

Programmer
Nov 2, 2000
12
0
0
NL
I need somekind of dialogue for selecting a directory. At this moment I use the standard Commondialog which works, but I can't get it configured for just selecting a path. So no open-file-box or save-file-box, but just the directory-tree and an OK- and Cancel-button.
 
Hi

You don't have to use the CommonDialog
I know of @ least 2 ways

1) DirListBox that's part of the default controls that are listed in your toolbox.
The nice thing about this control is that you don't have to include any references or components as it's intrinsic to Visual Basic & you don't have to worry about licensing.
It's also very easy to use (like a list box) It has its limitations though.

The other method is bit more trickier (but more powerful)
2) A few API calls can add that extra power to your apps and this is no exception
SHBrowseForFolder
SHGetPathFromIDList

are the two functions we'll be looking at.

Here's the code

Option Explicit

Private Enum BrowseModeEnum
BrowseForFolders& = 0
BrowseForFoldersAndFiles& = 1
BrowseForComputers& = 2
BrowseForPrinters& = 3
End Enum

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 Type SHITEMID
cb As Long
abID As Byte
End Type

Private Type ITEMIDLIST
mkid As SHITEMID
End Type

' WARNING:Some of these constants only work with
' certain versions of Shell32.dll.

'Only return computers. If the user selects anything
'other than a computer, the OK button is grayed.

Private Const BIF_BROWSEFORCOMPUTER = &H1000
'Only return printers. If the user selects anything
'other than a printer, the OK button is grayed.

Private Const BIF_BROWSEFORPRINTER = &H2000
'The browse dialog will display files as well as folders.

Private Const BIF_BROWSEINCLUDEFILES = &H4000
'Do not include network folders below the domain

Private Const BIF_DONTGOBELOWDOMAIN = &H2
'Include an edit control in the dialog box.

Private Const BIF_EDITBOX = &H10
'Use the new user-interface providing the user with a larger
'resizable dialog box which includes drag and drop, reordering,
'context menus, new folders, delete, and other context menu
'commands.

Private Const BIF_NEWDIALOGSTYLE = &H40
'Only return file system ancestors. If the user
'selects anything other than a file system ancestor,
'the OK button is grayed.

Private Const BIF_RETURNFSANCESTORS = &H8
'Only return file system directories. If the user selects
'folders that are not part of the file system, the OK
'button is grayed.

Private Const BIF_RETURNONLYFSDIRS = &H1
'Include a status area in the dialog box. The callback
'function can set the status text by sending messages
'to the dialog box.

Private Const BIF_STATUSTEXT = &H4
Private Const BIF_USENEWUI = (BIF_NEWDIALOGSTYLE Or BIF_EDITBOX)
Private 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
'The main API dec

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 SHGetSpecialFolderLocation Lib "Shell32.dll" _
(ByVal hwndOwner As Long, ByVal nFolder As Long, _
pidl As ITEMIDLIST) As Long
Private Declare Function OleInitialize Lib "ole32.dll" (lp As Any) As Long
Private Declare Sub OleUninitialize Lib "ole32" ()

Private Enum CLASSIDEnum
CSIDL_DESKTOP& = &H0 'Virtual Folder
CSIDL_PROGRAMS& = &H2
CSIDL_CONTROLS& = &H3 ' Control Panel.
CSIDL_PRINTERS& = &H4
CSIDL_PERSONAL& = &H5 '?My Documents?
CSIDL_FAVORITES& = &H6
CSIDL_STARTUP& = &H7 'StartUp Folder
CSIDL_RECENT& = &H8
CSIDL_SENDTO& = &H9
CSIDL_BITBUCKET& = &HA ' Recycle Bin.
CSIDL_STARTMENU& = &HB ' Start menu items.
CSIDL_DESKTOPDIRECTORY& = &H10 'Real Desktop Folder
CSIDL_DRIVES& = &H11 ' Virtual Folder (My Computer)
CSIDL_NETWORK& = &H12 ' Network Neighborhood (both)
CSIDL_NETHOOD& = &H13
CSIDL_FONTS& = &H14
CSIDL_TEMPLATES& = &H15
CSIDL_COMMON_STARTMENU& = &H16 'All Users menus (all 4)
CSIDL_COMMON_PROGRAMS& = &H17
CSIDL_COMMON_STARTUP& = &H18
CSIDL_COMMON_DESKTOPDIRECTORY& = &H19
CSIDL_APPDATA& = &H1A
CSIDL_PRINTHOOD& = &H1B
End Enum

Private me_BrowseMode As BrowseModeEnum
'End Of General Declarations
Private Function mf_GetSpecialFolder(CSIDL As CLASSIDEnum, IDL As ITEMIDLIST) As String

Dim sPath As String

mf_GetSpecialFolder = ""

If SHGetSpecialFolderLocation(Me.hWnd, CSIDL, IDL) = 0 Then
sPath = Space(MAX_PATH)

If SHGetPathFromIDList(ByVal IDL.mkid.cb, ByVal sPath) Then
mf_GetSpecialFolder = Left(sPath, InStr(sPath, vbNullChar) - 1) & "\"
End If
End If

End Function
Public Function ShowBrowseDialog(hwndOwner As Long, sPrompt As String) As String

Dim iNull As Integer
Dim lpIDList As Long
Dim lResult As Long
Dim sPath As String
Dim sPath1 As String
Dim udtBI As BrowseInfo
Dim IDL As ITEMIDLIST

' Get the ID of the folder to use as the root
' in the directory box. Change the "CSIDL_"
' constant to any of the defined values as
' shown in the declarations section of this form.

sPath1 = mf_GetSpecialFolder(CSIDL_DESKTOP, IDL)
'Initialize Drag & Drop

Call OleInitialize(ByVal 0&)

With udtBI
.pIDLRoot = IDL.mkid.cb '0 to start from Desktop
.hwndOwner = hwndOwner
.lpszTitle = lstrcat(sPrompt, "")

Select Case me_BrowseMode
Case BrowseForFolders
.ulFlags = BIF_RETURNONLYFSDIRS Or BIF_USENEWUI
Case BrowseForFoldersAndFiles
.ulFlags = BIF_RETURNONLYFSDIRS Or BIF_BROWSEINCLUDEFILES
Case BrowseForComputers
.ulFlags = BIF_BROWSEFORCOMPUTER
Case BrowseForPrinters
.ulFlags = BIF_BROWSEFORPRINTER
End Select
End With

lpIDList = SHBrowseForFolder(udtBI)

If lpIDList Then
sPath = String(MAX_PATH, 0)
lResult = SHGetPathFromIDList(lpIDList, sPath)
Call CoTaskMemFree(lpIDList)

iNull = InStr(sPath, vbNullChar)

If iNull Then
sPath = Left(sPath, iNull - 1)
End If
End If

Call OleUninitialize
ShowBrowseDialog = sPath

End Function
Private Sub Form_Load()

Const DIALOGPROMPT As String = "Click on an entry to select it."

me_BrowseMode = BrowseForFolders

MsgBox ShowBrowseDialog(hWnd, DIALOGPROMPT)

End Sub

Have fun
caf

 
Thanks caf.

I think the second method would be a little overdone for the simple task I need, but the first one is OK (and so obvious I oversaw it).

Kind regards,

K
 
I have pretty much what 'caf' wrote (using SHBrowseForFolder) in my own code. I am trying to find out exactly where the path info is stored so I can pass it back into one of the stuctures and setup the last opened or default folder.

I don't want to set the root since it will only display the tree from the root down. Anyone know how to do this?

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top