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

Have user choose a directory from a dialog box 6

Status
Not open for further replies.

mdav2

Programmer
Aug 22, 2000
363
GB
I need the user to use an explorer type dialog box to select a directory where a file will be open/saved. I have the code to do this in Access and Visual Foxpro but neither work in VB. I think it should be a fairly common task.

If anyone can help I would be most appreciative.
 
Use either the common dialog control or a DirListBox and a DriveListBox

(menu PROJECT|COMPONENTS - "Microsoft Common Dialog Controls 6.0" for the common dialog control)
 


Yes simple indeed

One can use the common dialog control which presents the standarddialog you see in windows APpps

Or there are seperate controls to list drives, directories and files that can be used to create a more custom dialog


Good luck
 
Try the DriveList control, the DirList control and the Filelist control. They are all in the default controls that load with any new project.
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Sorry CCLINT, I went for cuppa between reading it and answering it. Oh, the problems of 'calls of nature'
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Here is the smart, Windows Explorer enabled directory picker dialog.

Add a code module and paste the following code in it.
-----------------------------------------------------------

Option Explicit
Const MAX_PATH = 260
Const BIF_RETURNONLYFSDIRS = &H1&
Const BIF_STATUSTEXT = &H4
Const WM_USER = &H400
Const BFFM_INITIALIZED = 1
Const BFFM_SELCHANGED = 2
Const BFFM_SETSTATUSTEXT = WM_USER + 100
Const BFFM_SETSELECTION = WM_USER + 102
Const SHGFI_DISPLAYNAME = &H200
Const SHGFI_PIDL = &H8
Const WM_SETTEXT = &HC
Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Declare Function BrowseForFolder Lib "shell32" Alias "SHBrowseForFolder" (lpbi As BROWSEINFO) As Long
Declare Function GetPathFromIDList Lib "shell32" Alias "SHGetPathFromIDList" (ByVal pidl As Long, ByVal pszPath As String) As Long
Declare Function SHGetFileInfo Lib "shell32" Alias "SHGetFileInfoA" (ByVal pszPath As Any, ByVal dwFileAttributes As Long, psfi As SHFILEINFO, ByVal cbFileInfo As Long, ByVal uFlags As Long) As Long
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Type BROWSEINFO
hwndOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As String
iImage As Long
End Type
Type SHFILEINFO
hIcon As Long
iIcon As Long
dwAttributes As Long
szDisplayName As String * MAX_PATH
szTypeName As String * 80
End Type
Function BrowseCallbackProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal lParam As Long, ByVal lpData As String) As Long
Dim Path As String * MAX_PATH
If uMsg = BFFM_SELCHANGED Then
GetPathFromIDList lParam, Path
If Asc(Path) = 0 Then
Dim sfi As SHFILEINFO
SHGetFileInfo lParam, 0, sfi, Len(sfi), SHGFI_DISPLAYNAME Or SHGFI_PIDL
Path = sfi.szDisplayName
End If
SendMessage hwnd, BFFM_SETSTATUSTEXT, 0&, ByVal Path
ElseIf uMsg = BFFM_INITIALIZED Then
SendMessage hwnd, BFFM_SETSELECTION, True, ByVal lpData
SendMessage hwnd, WM_SETTEXT, 0, ByVal "Browse for Folder"
End If
End Function

Public Function BrowseFolder(StartFolder As String) As String
Dim bi As BROWSEINFO, pidl As Long, Path As String * MAX_PATH
CopyMemory bi.lpfn, AddressOf BrowseCallbackProc, 4
bi.ulFlags = BIF_RETURNONLYFSDIRS Or BIF_STATUSTEXT
bi.hwndOwner = Screen.ActiveForm.hwnd
bi.lParam = StrConv(Trim$(StartFolder), vbUnicode)
bi.lpszTitle = "Select a folder from the tree."
pidl = BrowseForFolder(bi)
If pidl Then
GetPathFromIDList pidl, Path
BrowseFolder = Left$(Path, InStr(Path, vbNullChar) - 1)
Else
BrowseFolder = StartFolder
End If
End Function

-----------------------------------------------------------
Now add a command button and a text box to your form and
paste the following code in your form.
-----------------------------------------------------------

Option Explicit
Private Sub Form_Load()
Text1 = App.Path
End Sub

Private Sub Command1_Click()
Text1 = BrowseFolder(Text1)
End Sub

-----------------------------------------------------------
Run the program and click the button to bring the dialog.
 
mdav: The code I pasted above is meant for browsing folders (as you asked) not for files. For browsing files, use Microsoft Common Dialog Control.
 
How would you fix that dialog into a form?
Perhaps put it in a picture box somehow?

I saw something on VB Accelerator, but it was overly
complicated and I couldnt work out which bits I could leave out.
 
Guys... I am very impressed and thankful with such replies.

I can go to MSDN library and found ShBrowseForFolder and others tool, but they are in all in C++ structures.
Where can I go to get the strucure and function written in Visual Basic 6.0. For example the structure for type BrowseInfo and function for BrowseForFolder?? I have tried microsoft knowledge base, and I just could not find it. Help!!

Thanks
 
That's what hypetia's answer above does!

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
This is all great though I require more information on DirList control and the Filelist control, could soemeone please help me out.
NOTE: I am aware of the MS common dialog control, I do not want to use this.
 
Or you could use the code I posted in Thread222-573084, which has the minor advantage of being able to deal with all of the Windows special folders as well as normal folders
 
Hypetia,
Thanks for some great code!!! :) Here's a star for you.

Take Care,
Mike
 
Just my $0.02 in re synchronization of DriveListBox and DirListBox:

Form Objects are:
WatchedDrive
WatchedDir
My objects are:
xFileMgmt (module)
xFileMgmt.xWF (TYPE struct in module)
Note: in our environment the label on network drives show up in the WatchedDrive object, therefore had to parse them off (ie: u: [\\someserver\someshare login]). Also, my object xFileMgmt has additional methods and properties for parsing file paths, etc., to look like shell & TCL/TK parsing functions.


Private Sub WatchedDrive_Change()

x = Split(WatchedDrive.Drive, " ")
xFileMgmt.xWF.ROOTdrive = x(0)
xFileMgmt.xWF.ROOTdir = x(0) + "\"
WatchedDir.Path = xFileMgmt.xWF.ROOTdir

End Sub

John Lopez
Enterprise PDM Architect
john.l.lopez@goodrich.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top