Visual Basic Explorer
Visual Basic Explorer
 Navigation
 Home


 Coding
 Source Code

 FAQ Center

 VB Tips

 Downloads

 ToolBox

 Tutorials

 VB Games

 VB News

 VB Award

 VB Forums



 Affiliates
 Planet Source Code

 Rent a Coder

 DirectX4VB


 Misc
 Search

 Feedback

 Advertise

 About


Need to hire
a VB coder?

Please support our sponsor:

 Home 
 Site Map 
 Forums 
 News 
 Feedback 

Using Common Dialog to Select Directories Instead of Filess

I 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.
--------------------------------------------

Download VB4 Sample





Home | About | What's New | Source Code | FAQ | Tips & Tricks | Downloads | ToolBox | Tutorials | Game Programming | VB Award | Search | VB Forums | Feedback | VBNews | Copyright & Disclaimer | Advertise | Privacy Policy |

Quick searches: Site Search | Advanced Site Search 

Copyright 2002 by Exhedra Solutions, Inc.
By using this site you agree to its terms and conditions
VB Explorer and VBExplorer.com are trademarks of Exhedra Solutions, Inc.