1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

VB6 - Selecting folders in CommonDialog

Discussion in 'Software Development' started by asbo, Feb 18, 2003.

Thread Status:
Not open for further replies.
  1. asbo

    asbo Thread Starter

    Sep 21, 2001

    I'm writing a piece of software that requires the user to select a directory -- NOT a file -- for use in a TextBox. I know how to handle the CmnDlg's output and such, but how can I make it so that the user can select a folder, as appose to a file? I've the "cdlOFNExplorer" Flag to no sucess. Can anyone help me out with this? I've pretty much scoured MSDN, and can't find anything related to this.

  2. Mosaic1


    Aug 17, 2001
    Select a Directory as output to a text box? Not sure what you are asking. Do you want the name of the folder to show in the text box? Or the contents of the folder?
    I think you may have to use the DirList box and not the FILE save dialog.
  3. asbo

    asbo Thread Starter

    Sep 21, 2001

    I cleaned up a little of the wording above; see if that helps. A visual rep of what I'm talking about is kinda like when you've got an explorer window open (say to C:\) and you click the Folders option on the toolbar, only that that, stick in a dialog and put OK an Cancel beneath it.

    That nay better?

  4. TimCottee


    Oct 10, 2001
    This is what you are looking for:

    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
    Const MAX_PATH = 260
    Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
    Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
    Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
    Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long
    Private Sub Form_Load()
        'KPD-Team 1998
        'URL: [url]http://www.allapi.net/[/url]
        '[email protected]
        Dim iNull As Integer, lpIDList As Long, lResult As Long
        Dim sPath As String, udtBI As BrowseInfo
        With udtBI
            'Set the owner window
            .hWndOwner = Me.hWnd
            'lstrcat appends the two strings and returns the memory address
            .lpszTitle = lstrcat("C:\", "")
            'Return only if the user selected a directory
            .ulFlags = BIF_RETURNONLYFSDIRS
        End With
        'Show the 'Browse for folder' dialog
        lpIDList = SHBrowseForFolder(udtBI)
        If lpIDList Then
            sPath = String$(MAX_PATH, 0)
            'Get the path from the IDList
            SHGetPathFromIDList lpIDList, sPath
            'free the block of memory
            CoTaskMemFree lpIDList
            iNull = InStr(sPath, vbNullChar)
            If iNull Then
                sPath = Left$(sPath, iNull - 1)
            End If
        End If
        MsgBox sPath
    End Sub
  5. asbo

    asbo Thread Starter

    Sep 21, 2001

    I was waiting for you to come! ;) Thanks again. Any suggestions on possibly turning this into its own Sub, so I can just call on it when I need it?


    [EDIT: Nevermind the Sub part; I actually figured something out for my self once :D :p. Thanks again!!]
  6. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/119626

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice