Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select a folder only by using common dialog control

Tags:

vb6

Using VB6

Code.

CommonDialog1.DialogTitle = "Open File"
CommonDialog1.Filter = "*.*"
CommonDialog1.FilterIndex = 1
CommonDialog1.Flags = cdlOFNAllowMultiselect + cdlOFNExplorer
CommonDialog1.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
CommonDialog1.CancelError = True
On Error Resume Next
CommonDialog1.ShowOpen
If Err Then
    'MsgBox "Select Folder"
    Exit Sub
End If

From the above code, i am selecting a file, But i don't want to select a file, I want to select only the folder. How to modify my code.

Need vb6 code Help?

like image 311
Gopal Avatar asked Oct 18 '09 04:10

Gopal


2 Answers

To select a folder, you can use the Shell and Automation Component.

Private shlShell As Shell32.Shell
Private shlFolder As Shell32.Folder
Private Const BIF_RETURNONLYFSDIRS = &H1

Private Sub Command1_Click()
    If shlShell Is Nothing Then
        Set shlShell = New Shell32.Shell
    End If
    Set shlFolder = shlShell.BrowseForFolder(Me.hWnd, "Select a Directory", BIF_RETURNONLYFSDIRS)
    If Not shlFolder Is Nothing Then
        MsgBox shlFolder.Title
    End If
End Sub

You will need to add a reference to shell32.dll to your project. Use the Project/References... menu and then browse for shell32.dll.

Or you can use the Windows API as Twotymz suggests.

like image 54
Robert Harvey Avatar answered Nov 17 '22 10:11

Robert Harvey


It's been a while since I've had to do any visual basic work but I think instead of using the common dialog box for getting the name of a file to open you should use the SHBrowseForFolder function which is already part of the Windows API. Here's a link to a page that describes it's usage.

Update (2017): Provided link is broken but a backed-up version can be viewed on archive.org

like image 8
Twotymz Avatar answered Nov 17 '22 11:11

Twotymz