Results 1 to 10 of 10

Thread: [resolved]selecting directory (commondialog thingy)

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2002
    Location
    Blue Mountains NSW Australia
    Posts
    160

    Smile [resolved]selecting directory (commondialog thingy)

    um i know how to select a file by adding the commondialog component, is selecting a directory similar? and can some tell me how ? thanks.
    Last edited by Jack Daniels; Jul 31st, 2002 at 08:36 AM.
    Jack Daniels
    ICQ: 72074030
    VB 6.0

  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    VB Code:
    1. Option Explicit
    2.  
    3. Public Type BrowseInfo
    4.      hwndOwner As Long
    5.      pIDLRoot As Long
    6.      pszDisplayName As Long
    7.      lpszTitle As Long
    8.      ulFlags As Long
    9.      lpfnCallback As Long
    10.      lParam As Long
    11.      iImage As Long
    12. End Type
    13.  
    14. Public Const BIF_RETURNONLYFSDIRS = 1
    15. Public Const BIF_NEWDIALOGSTYLE = &H40
    16.  
    17. Public Const MAX_PATH = 260
    18. Public Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
    19. Public Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
    20. Public Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
    21. Public Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long
    22.  
    23. Public Function BrowseForFolder(hwndOwner As Long, sPrompt As String) As String
    24.      
    25.     'declare variables to be used
    26.      Dim iNull As Integer
    27.      Dim lpIDList As Long
    28.      Dim lResult As Long
    29.      Dim sPath As String
    30.      Dim udtBI As BrowseInfo
    31.  
    32.     'initialise variables
    33.      With udtBI
    34.         .hwndOwner = hwndOwner
    35.         .lpszTitle = lstrcat(sPrompt, "")
    36.         .ulFlags = BIF_RETURNONLYFSDIRS + BIF_NEWDIALOGSTYLE
    37.      End With
    38.  
    39.     'Call the browse for folder API
    40.      lpIDList = SHBrowseForFolder(udtBI)
    41.      
    42.     'get the resulting string path
    43.      If lpIDList Then
    44.         sPath = String$(MAX_PATH, 0)
    45.         lResult = SHGetPathFromIDList(lpIDList, sPath)
    46.         Call CoTaskMemFree(lpIDList)
    47.         iNull = InStr(sPath, vbNullChar)
    48.         If iNull Then sPath = Left$(sPath, iNull - 1)
    49.      End If
    50.  
    51.     'If cancel was pressed, sPath = ""
    52.      BrowseForFolder = sPath
    53.  
    54. End Function
    55.  
    56.  
    57. 'usage :
    58. Private Sub Form_Click()
    59.     MsgBox BrowseForFolder(Me.hWnd, "select folder")
    60. End Sub

    should do what you want
    -= a peet post =-

  3. #3
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545
    If you want the user to select a file using the CommonDialog control add this code to your project and add a commonDialog control to your form


    VB Code:
    1. Private Function ShowFileDialog() As String
    2.     With CommonDialog1
    3.         .CancelError = True
    4.         .DialogTitle = "Select Datasource"
    5.         .Filter = "Microsoft Access Databases(*.mdb)|*.mdb"
    6.         .InitDir = App.Path
    7.         On Error GoTo cancel_error  'Need this in case user hits Cancel button!
    8.         .ShowOpen
    9.         ShowFileDialog = .fileName
    10.     End With
    11.     Exit Sub
    12. cancel_error:
    13.     If Err.Number <> cdlCancel Then
    14.         'an error occured somewhere
    15.         MsgBox "Error #" & err.Number & vbcrlf & err.Description
    16.     End If
    17.     ShowFileDialog = "" 'Return an empty string
    18. End Function


    If, on the other hand, you want to allow the user to choose a folder you can do this.

    Add this to a module

    VB Code:
    1. Option Explicit
    2.  
    3. Public Type BrowseInfo
    4.      hwndOwner As Long
    5.      pIDLRoot As Long
    6.      pszDisplayName As Long
    7.      lpszTitle As Long
    8.      ulFlags As Long
    9.      lpfnCallback As Long
    10.      lParam As Long
    11.      iImage As Long
    12. End Type
    13.  
    14. Public Const BIF_RETURNONLYFSDIRS = 1
    15. Public Const BIF_NEWDIALOGSTYLE = &H40
    16.  
    17. Public Const MAX_PATH = 260
    18. Public Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
    19. Public Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
    20. Public Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
    21. Public Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long
    22.  
    23. Public Function BrowseForFolder(hwndOwner As Long, sPrompt As String) As String
    24.      
    25.     'declare variables to be used
    26.      Dim iNull As Integer
    27.      Dim lpIDList As Long
    28.      Dim lResult As Long
    29.      Dim sPath As String
    30.      Dim udtBI As BrowseInfo
    31.  
    32.     'initialise variables
    33.      With udtBI
    34.         .hwndOwner = hwndOwner
    35.         .lpszTitle = lstrcat(sPrompt, "")
    36.         .ulFlags = BIF_RETURNONLYFSDIRS + BIF_NEWDIALOGSTYLE
    37.      End With
    38.  
    39.     'Call the browse for folder API
    40.      lpIDList = SHBrowseForFolder(udtBI)
    41.      
    42.     'get the resulting string path
    43.      If lpIDList Then
    44.         sPath = String$(MAX_PATH, 0)
    45.         lResult = SHGetPathFromIDList(lpIDList, sPath)
    46.         Call CoTaskMemFree(lpIDList)
    47.         iNull = InStr(sPath, vbNullChar)
    48.         If iNull Then sPath = Left$(sPath, iNull - 1)
    49.      End If
    50.  
    51.     'If cancel was pressed, sPath = ""
    52.      BrowseForFolder = sPath
    53.  
    54. End Function

    and use it like this
    VB Code:
    1. Private Sub Command1_Click()
    2.     MsgBox BrowseForFolder(Me.hWnd, "select folder")
    3. End Sub

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Apr 2002
    Location
    Blue Mountains NSW Australia
    Posts
    160
    Thanks :-) im shocking with API's, atleast XP does something now lol.
    Jack Daniels
    ICQ: 72074030
    VB 6.0

  5. #5
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545
    Damn you peet I copied that code from one of your posts LOL and then YOU beat me to it

  6. #6
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Originally posted by ae_jester
    Damn you peet I copied that code from one of your posts LOL and then YOU beat me to it
    hehe... I didn't have to search for my post..
    -= a peet post =-

  7. #7
    Addicted Member
    Join Date
    Apr 1999
    Location
    Ruinen, Drente, Netherlands
    Posts
    192
    I am using the nice code from Peet, my question:
    How start with a specific folder instead of the root (C:\) ??

  8. #8
    New Member
    Join Date
    May 2009
    Posts
    2

    Re: [resolved]selecting directory (commondialog thingy)

    Wow! This code work great! Thanks

  9. #9
    New Member
    Join Date
    May 2009
    Posts
    2

    Re: [resolved]selecting directory (commondialog thingy)

    Wow! This code work great! Thanks

  10. #10
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: [resolved]selecting directory (commondialog thingy)

    Thanks for letting us know that you have your answer. Are you aware that if you have JavaScript enabled you can do that easily by pulling down the Thread Tools menu and selecting the Mark Thread Resolved item? Also if someone has been particularly helpful you have the ability to affect their forum "reputation" by rating their post. Only those ratings that you give after you have 20 posts will actually count, but in all cases the person you rate will see your rating and know that you appreciate their help.

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