|
Using Common Dialog to Select Directories Instead of FilessI had an occasion 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. I don't know if anyone would find this interesting or useful, but here goes: Set the common dialog control (called "cdMain") using:
'-- Initialize Common Dialog control
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 seting 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:
Private Sub btnBrowse_Click()
Dim x As Integer
'-- Cheap way to use the common dialog box as a directory-picker
x = 3
cdMain.CancelError = True 'do not terminate on error
On Error Resume Next 'I will hande errors
cdMain.Action = 1 '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
ChDrive cdMain.filename
txtPath.Text = Left(cdMain.filename, Len(cdMain.filename) - x)
Else
'-- User pressed "Cancel"
End If
End Sub
-------------------------------------------- Original tip and project submitted by Tom Kumpf. Minor text editing by Burt Abreu. --------------------------------------------
|
Quick searches: Site Search | Advanced Site Search |
|
By using this site you agree to its terms and conditions VB Explorer and VBExplorer.com are trademarks of Exhedra Solutions, Inc. |