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

use common dialog to allow selecting of a folder

Status
Not open for further replies.

ri

Programmer
Nov 2, 2000
12
GB
is this poss and how do i do it?
 
I don't know if that's possible but you could use the DriveListBox and DirListBox to achieve this.

These are default controls that are loaded with Visual basic (in other words, no other References... or Components... need to be added).

Look them up in your MSDN.
 
I do not think this is possible with the common dialog control. As jkb17 says, use the DirListBox. If you want to change the drive that this list points to, use the DriveListBox and use the code below to refresh the DirListBox:

Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
Dir1.Refresh
End Sub


Simon
 
Here are two useful methods that don't require any kind of control:

Method #1
[tt]
Private Declare Function ShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal _
lpOperation As String, ByVal lpFile As String, ByVal _
lpParameters As String, ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long

Private Const SW_SHOWNORMAL = 1

Public Sub OpenDirectory(Directory As String)
ShellExecute 0, "Open", Directory, vbNullString, vbNullString, SW_SHOWNORMAL
End Sub

Public Sub OpenExplorer(Optional InitialDirectory As String)
ShellExecute 0, "Explore", InitialDirectory, vbNullString, vbNullString, SW_SHOWNORMAL
End Sub
[/tt]
Usage:[tt]
OpenDirectory ("C:\")
OpenExplorer
OpenExplorer ("C:\Program Files\")
[/tt]
Method #2
[tt]
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

Public Enum BrowseType
BrowseForFolders = &H1
BrowseForComputers = &H1000
BrowseForPrinters = &H2000
BrowseForEverything = &H4000
End Enum

Public Enum FolderType
CSIDL_BITBUCKET = 10
CSIDL_CONTROLS = 3
CSIDL_DESKTOP = 0
CSIDL_DRIVES = 17
CSIDL_FONTS = 20
CSIDL_NETHOOD = 18
CSIDL_NETWORK = 19
CSIDL_PERSONAL = 5
CSIDL_PRINTERS = 4
CSIDL_PROGRAMS = 2
CSIDL_RECENT = 8
CSIDL_SENDTO = 9
CSIDL_STARTMENU = 11
End Enum

Private Const MAX_PATH = 260
Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
Private Declare Function lstrcat Lib "kernel32.dll" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
Private Declare Function SHBrowseForFolder Lib "shell32.dll" (lpbi As BrowseInfo) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32.dll" (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, ListId As Long) As Long
Public Function BrowseFolders(hWndOwner As Long, sMessage As String, Browse As BrowseType, ByVal RootFolder As FolderType) As String
Dim Nullpos As Integer
Dim lpIDList As Long
Dim res As Long
Dim sPath As String
Dim BInfo As BrowseInfo
Dim RootID As Long
SHGetSpecialFolderLocation hWndOwner, RootFolder, RootID
BInfo.hWndOwner = hWndOwner
BInfo.lpszTitle = lstrcat(sMessage, "")
BInfo.ulFlags = Browse
If RootID <> 0 Then BInfo.pIDLRoot = RootID
lpIDList = SHBrowseForFolder(BInfo)
If lpIDList <> 0 Then
[tab]sPath = String(MAX_PATH, 0)
[tab]res = SHGetPathFromIDList(lpIDList, sPath)
[tab]Call CoTaskMemFree(lpIDList)
[tab]Nullpos = InStr(sPath, vbNullChar)
[tab]If Nullpos <> 0 Then
[tab][tab]sPath = Left(sPath, Nullpos - 1)
[tab]End If
End If
BrowseFolders = sPath
End Function

Private Sub Form_Click()
MsgBox BrowseFolders(hWnd, &quot;Select a Folder&quot;, BrowseForFolders, CSIDL_NETHOOD)
End Sub
[/tt]
VCA.gif

Alt255@Vorpalcom.Intranets.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top