Results 1 to 19 of 19

Thread: selecting folders only with common dialog control

  1. #1

    Thread Starter
    Hyperactive Member Colonel Klink's Avatar
    Join Date
    Aug 2002
    Location
    Gold Coast, Australia
    Posts
    329

    selecting folders only with common dialog control

    is this possible?
    and if so how?

  2. #2
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724
    VB Code:
    1. Private Type BrowseInfo
    2.     hWndOwner As Long
    3.     pIDLRoot As Long
    4.     pszDisplayName As Long
    5.     lpszTitle As Long
    6.     ulFlags As Long
    7.     lpfnCallback As Long
    8.     lParam As Long
    9.     iImage As Long
    10. End Type
    11.  
    12. Private Const BIF_RETURNONLYFSDIRS = 1
    13. Private Const MAX_PATH = 260
    14.  
    15. Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
    16. Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
    17. Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
    18. Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long
    19.  
    20. Public Function BrowseForFolder(ByVal strInitDrive As String, ByVal lngHwndOwner As Long) As String
    21.     'Opens the browse for folder dialog
    22.     Dim iNull As Integer, lpIDList As Long
    23.     Dim sPath As String, udtBI As BrowseInfo
    24.  
    25.     With udtBI
    26.         .hWndOwner = lngHwndOwner
    27.         'lstrcat appends the two strings and returns the memory address
    28.         .lpszTitle = lstrcat(strInitDrive, "")
    29.         'Return only if the user selected a directory
    30.         .ulFlags = BIF_RETURNONLYFSDIRS
    31.     End With
    32.  
    33.     'Show the 'Browse for folder' dialog
    34.     lpIDList = SHBrowseForFolder(udtBI)
    35.     If lpIDList Then
    36.         sPath = String$(MAX_PATH, 0)
    37.         'Get the path from the IDList
    38.         SHGetPathFromIDList lpIDList, sPath
    39.         'free the block of memory
    40.         CoTaskMemFree lpIDList
    41.         iNull = InStr(sPath, vbNullChar)
    42.         If iNull Then
    43.             sPath = Left$(sPath, iNull - 1)
    44.         End If
    45.     End If
    46.  
    47.     BrowseForFolder = sPath
    48. End Function

  3. #3

    Thread Starter
    Hyperactive Member Colonel Klink's Avatar
    Join Date
    Aug 2002
    Location
    Gold Coast, Australia
    Posts
    329
    lemme see if i got this right... heh


    BrowseForFolder is the end result and will return for example "C:\folder\folder2\" ?

    sorry
    me = VB super n00b
    heh

  4. #4
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724
    basibally, that's exactly what it does

  5. #5

    Thread Starter
    Hyperactive Member Colonel Klink's Avatar
    Join Date
    Aug 2002
    Location
    Gold Coast, Australia
    Posts
    329
    heh thanks

  6. #6

    Thread Starter
    Hyperactive Member Colonel Klink's Avatar
    Join Date
    Aug 2002
    Location
    Gold Coast, Australia
    Posts
    329
    heh sorry aboiut this... just one more thing... just say i have a command button: named Button1 that i wanna use when clicked opens up that dialog box

    how do i do that?

  7. #7

    Thread Starter
    Hyperactive Member Colonel Klink's Avatar
    Join Date
    Aug 2002
    Location
    Gold Coast, Australia
    Posts
    329
    anyone?

  8. #8
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724
    Put the browse code in a module &

    VB Code:
    1. Private Sub Command1_Click()
    2.   Dim strFolder As String
    3.  
    4.   strFolder = BrowserForFolder("C:\", Me.hWnd)
    5.  
    6.   MsgBox strFolder
    7. End Sub

  9. #9

    Thread Starter
    Hyperactive Member Colonel Klink's Avatar
    Join Date
    Aug 2002
    Location
    Gold Coast, Australia
    Posts
    329
    thank you

  10. #10

    Thread Starter
    Hyperactive Member Colonel Klink's Avatar
    Join Date
    Aug 2002
    Location
    Gold Coast, Australia
    Posts
    329
    sorry to do this to ya... i put in the code but it didnt work

    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

    Private Const BIF_RETURNONLYFSDIRS = 1
    Private 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

    Public Function BrowseForFolder(ByVal strInitDrive As String, ByVal lngHwndOwner As Long) As String
    'Opens the browse for folder dialog
    Dim iNull As Integer, lpIDList As Long
    Dim sPath As String, udtBI As BrowseInfo

    With udtBI
    .hWndOwner = lngHwndOwner
    'lstrcat appends the two strings and returns the memory address
    .lpszTitle = lstrcat(strInitDrive, "")
    '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

    BrowseForFolder = sPath
    End Function


    i put into a module

    and

    Dim strFolder As String

    strFolder = BrowserForFolder("C:\", Me.hWnd)

    MsgBox strFolder

    i put into the button but i get a "sub or function not defined" error and it highlights this line:

    strFolder = BrowseForFolder("C:\", Me.hWnd)

    and this is highlighted with an yellow arrow

    Private Sub ButRootFolder_Click()

    am i doin it right?

    also, do do you set the default path for the code above?
    Last edited by Colonel Klink; Aug 31st, 2002 at 07:36 AM.

  11. #11

    Thread Starter
    Hyperactive Member Colonel Klink's Avatar
    Join Date
    Aug 2002
    Location
    Gold Coast, Australia
    Posts
    329
    bumpage

  12. #12
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    The attached module is basically the same thing as what axion_sa has posted except it also lets you choose the default directory. The useage for it is:
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim strPath As String
    3.  
    4.     strPath = GetFolderPath(Me, "C:\program files")
    5.     MsgBox strPath
    6. End Sub
    The default directory is optional and will open to the c drive if left blank.
    Attached Files Attached Files

  13. #13

    Thread Starter
    Hyperactive Member Colonel Klink's Avatar
    Join Date
    Aug 2002
    Location
    Gold Coast, Australia
    Posts
    329
    strPath = GetFolderPath(Me, "C:\program files")

    the "C:\Program Files" is the default directory yes?

  14. #14
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    Yes. Change that to what ever you need or just don't use that parameter.

  15. #15

    Thread Starter
    Hyperactive Member Colonel Klink's Avatar
    Join Date
    Aug 2002
    Location
    Gold Coast, Australia
    Posts
    329
    thanks

  16. #16
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    you typoed 'Browse'

    VB Code:
    1. Dim strFolder As String
    2.  
    3. strFolder = [COLOR=red]Browser[/COLOR]ForFolder("C:\", Me.hWnd)
    4.  
    5. MsgBox strFolder

    that's why you were getting that error.
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  17. #17
    Lively Member
    Join Date
    Aug 2009
    Posts
    98

    Re: selecting folders only with common dialog control

    Suppose first time I selected folder "D:\Test". Now when I go for browsing again, I want that in browser window folder "D:\Test" is preselected and I should be able to either select a folder above that (i.e. its parent folder) or a folder below that (i.e. its sub folder). How to do this? i.e. when I go for browsing a folder earlier selected folder is shown preselected in this window.

  18. #18
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: selecting folders only with common dialog control

    Requires setting the lpfnCallback member of the BrowseInfo structure and then responding to messages it sends you. Suggest searching forum and/or google for: BrowseInfo lpfnCallback BFFM_SETSELECTION

    Here is one example to get you started: http://vbcity.com/forums/t/80178.aspx
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  19. #19
    Lively Member
    Join Date
    Aug 2009
    Posts
    98

    Re: selecting folders only with common dialog control

    Thanks LaVolpe

    With slight modifications it worked.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width