Results 1 to 5 of 5

Thread: [RESOLVED] Selecting a Folder, Common Dialog control?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2006
    Posts
    91

    Resolved [RESOLVED] Selecting a Folder, Common Dialog control?

    i want the user of my app to be able to select a folder that .txt files will be created in.

    i'd like to use this using the windows common dialog control, or something similar. i dont want to save files, or open files, i want to just select a folder, so my app knows where to make the files

  2. #2
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Selecting a Folder, Common Dialog control?

    Call the BrowseForFolder function like this:
    VB Code:
    1. BrowseForFolder Me.hWnd, "Select a folder to store the text files"
    and the actual function
    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_BROWSEFORCOMPUTER = &H1000
    13. Private Const BIF_BROWSEFORPRINTER = &H2000
    14. Private Const BIF_BROWSEINCLUDEFILES = &H4000
    15. Private Const BIF_BROWSEINCLUDEURLS = &H80
    16. Private Const BIF_DONTGOBELOWDOMAIN = &H2
    17. Private Const BIF_EDITBOX = &H10
    18. Private Const BIF_NEWDIALOGSTYLE = &H40
    19. Private Const BIF_RETURNFSANCESTORS = &H8
    20. Private Const BIF_RETURNONLYFSDIRS = &H1
    21. Private Const BIF_SHAREABLE = &H8000
    22. Private Const BIF_STATUSTEXT = &H4
    23. Private Const BIF_USENEWUI = &H40
    24. Private Const BIF_VALIDATE = &H20
    25.  
    26. Private Const MAX_PATH = 260
    27. Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
    28. Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
    29. Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
    30. Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long
    31.  
    32. Public Function BrowseForFolder(ByVal hwnd As Long, Optional ByVal Title As String = "Select a folder") As String
    33.     'KPD-Team 1998
    34.     'URL: [url]http://www.allapi.net/[/url]
    35.     Dim iNull As Integer, lpIDList As Long, lResult As Long
    36.     Dim sPath As String, udtBI As BrowseInfo
    37.    
    38.     With udtBI
    39.         .pszDisplayName = 0
    40.         .hWndOwner = hwnd 'Set the owner window
    41.         .lpszTitle = lstrcat(Title, "") 'lstrcat appends the two strings and returns the memory address
    42.         .ulFlags = BIF_RETURNONLYFSDIRS Or BIF_DONTGOBELOWDOMAIN Or BIF_NEWDIALOGSTYLE 'Return only if the user selected a directory
    43.     End With
    44.    
    45.     lpIDList = SHBrowseForFolder(udtBI) 'Show the 'Browse for folder' dialog
    46.    
    47.     If lpIDList Then
    48.         sPath = String$(MAX_PATH, 0)
    49.        
    50.         SHGetPathFromIDList lpIDList, sPath 'Get the path from the IDList
    51.        
    52.         CoTaskMemFree lpIDList 'free the block of memory
    53.        
    54.         iNull = InStr(sPath, vbNullChar)
    55.         If iNull Then sPath = Left$(sPath, iNull - 1)
    56.     End If
    57.    
    58.     BrowseForFolder = sPath
    59. End Function

  3. #3
    Lively Member
    Join Date
    Feb 2005
    Posts
    103

    Re: [RESOLVED] Selecting a Folder, Common Dialog control?

    Is there a way to set the initial folder w/ this code?

    Also, can you restrict it to folders a user can write to, or do you have to check that once he/she chooses the folder?

  4. #4
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  5. #5
    Lively Member
    Join Date
    Feb 2005
    Posts
    103

    Re: [RESOLVED] Selecting a Folder, Common Dialog control?

    Thank you,
    -Bret

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