Results 1 to 4 of 4

Thread: Path Selection Dialog Box

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    45
    I was hoping to use the common dialog box for mere path selection (as opposed to actually having to pick a file)

    I'd like a "browse for path" dialog box. Are there any common controls that can do this and return only the path name.
    -Gregg
    -NoOBie At LaRg3

  2. #2
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    'Using the browser dialog box 
    'make a reference 
    
    Option Explicit 
    
    Private Const BIF_RETURNONLYFSDIRS = 1 
    Private Const BIF_DONTGOBELOWDOMAIN = 2 
    Private Const MAX_PATH = 260 
    
    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 Declare Function lstrcat Lib "kernel32" _ 
    Alias "lstrcatA" (ByVal lpString1 As String, ByVal _ 
    lpString2 As String) As Long 
    
    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 Sub Form_Load() 
    'Opens a Browse Folders Dialog Box that displays the 
    'directories in your computer 
    Dim lpIDList As Long ' Declare Varibles 
    Dim sBuffer As String 
    Dim szTitle As String 
    Dim tBrowseInfo As BrowseInfo 
    
    szTitle = "Hello World. Click on a directory and " & _ 
    "it's path will be displayed in a message box" 
    ' Text to appear in the the gray area under the title bar 
    ' telling you what to do 
    
    With tBrowseInfo 
    .hWndOwner = Me.hWnd ' Owner Form 
    .lpszTitle = lstrcat(szTitle, "") 
    .ulFlags = BIF_RETURNONLYFSDIRS + BIF_DONTGOBELOWDOMAIN 
    End With 
    
    lpIDList = SHBrowseForFolder(tBrowseInfo) 
    
    If (lpIDList) Then 
    sBuffer = Space(MAX_PATH) 
    SHGetPathFromIDList lpIDList, sBuffer 
    sBuffer = Left(sBuffer, InStr(sBuffer, vbNullChar) - 1) 
    
    ' <<< Add Your Code Here >>> 
    
    MsgBox sBuffer 
    
    End If 
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  3. #3
    Addicted Member Shrog's Avatar
    Join Date
    Aug 1999
    Location
    Darkest Africa
    Posts
    186

    How do I give it a starting directory?

    This is what I've been using, but I cannot find out how to get the dialog window to start up at a specific directory.

    I want to be able to take the user directly to the last selected directory, but instead the dialog starts at the root directory, and the user has to navigate through the directories each time.

    Does anybody know how to do this?

    Shrog
    VB6 Ent SP5
    Win2000

  4. #4
    Fanatic Member faisalkm's Avatar
    Join Date
    Oct 2000
    Location
    Germany
    Posts
    752
    vb Code:
    1. 'Paste this code in a Module
    2.  
    3. Public Type BROWSEINFO
    4.   hOwner As Long
    5.   pidlRoot As Long
    6.   pszDisplayName As String
    7.   lpszTitle As String
    8.   ulFlags As Long
    9.   lpfn As Long
    10.   lParam As Long
    11.   iImage As Long
    12. End Type
    13.  
    14. Public Declare Function SHBrowseForFolder Lib _
    15.    "shell32.dll" Alias "SHBrowseForFolderA" _
    16.    (lpBrowseInfo As BROWSEINFO) As Long
    17.  
    18. Public Declare Function SHGetPathFromIDList Lib _
    19.    "shell32.dll" Alias "SHGetPathFromIDListA" _
    20.    (ByVal pidl As Long, _
    21.    ByVal pszPath As String) As Long
    22.  
    23. Public Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal pv As Long)
    24.  
    25. Public Declare Function SendMessage Lib "user32" _
    26.    Alias "SendMessageA" _
    27.    (ByVal hWnd As Long, _
    28.    ByVal wMsg As Long, _
    29.    ByVal wParam As Long, _
    30.    lParam As Any) As Long
    31.    
    32. Public Declare Sub CopyMemory Lib "kernel32" _
    33.    Alias "RtlMoveMemory" _
    34.    (pDest As Any, _
    35.     pSource As Any, _
    36.     ByVal dwLength As Long)
    37.    
    38. Public Const MAX_PATH = 260
    39. Public Const WM_USER = &H400
    40. Public Const BFFM_INITIALIZED = 1
    41.  
    42. 'Constants ending in 'A' are for Win95 ANSI
    43. 'calls; those ending in 'W' are the wide Unicode
    44. 'calls for NT.
    45.  
    46. 'Sets the status text to the null-terminated
    47. 'string specified by the lParam parameter.
    48. 'wParam is ignored and should be set to 0.
    49. Public Const BFFM_SETSTATUSTEXTA As Long = (WM_USER + 100)
    50. Public Const BFFM_SETSTATUSTEXTW As Long = (WM_USER + 104)
    51.  
    52. 'If the lParam  parameter is non-zero, enables the
    53. 'OK button, or disables it if lParam is zero.
    54. '(docs erroneously said wParam!)
    55. 'wParam is ignored and should be set to 0.
    56. Public Const BFFM_ENABLEOK As Long = (WM_USER + 101)
    57.  
    58. 'Selects the specified folder. If the wParam
    59. 'parameter is FALSE, the lParam parameter is the
    60. 'PIDL of the folder to select , or it is the path
    61. 'of the folder if wParam is the C value TRUE (or 1).
    62. 'Note that after this message is sent, the browse
    63. 'dialog receives a subsequent BFFM_SELECTIONCHANGED
    64. 'message.
    65. Public Const BFFM_SETSELECTIONA As Long = (WM_USER + 102)
    66. Public Const BFFM_SETSELECTIONW As Long = (WM_USER + 103)
    67.    
    68.  
    69. 'specific to the PIDL method
    70. 'Undocumented call for the example. IShellFolder's
    71. 'ParseDisplayName member function should be used instead.
    72. Public Declare Function SHSimpleIDListFromPath Lib _
    73.    "shell32" Alias "#162" _
    74.    (ByVal szPath As String) As Long
    75.  
    76.  
    77. 'specific to the STRING method
    78. Public Declare Function LocalAlloc Lib "kernel32" _
    79.    (ByVal uFlags As Long, _
    80.     ByVal uBytes As Long) As Long
    81.    
    82. Public Declare Function LocalFree Lib "kernel32" _
    83.    (ByVal hMem As Long) As Long
    84.  
    85. Public Declare Function lstrcpyA Lib "kernel32" _
    86.    (lpString1 As Any, lpString2 As Any) As Long
    87.  
    88. Public Declare Function lstrlenA Lib "kernel32" _
    89.    (lpString As Any) As Long
    90.  
    91. Public Const LMEM_FIXED = &H0
    92. Public Const LMEM_ZEROINIT = &H40
    93. Public Const LPTR = (LMEM_FIXED Or LMEM_ZEROINIT)
    94.  
    95. 'windows-defined type OSVERSIONINFO
    96. Public Type OSVERSIONINFO
    97.   OSVSize         As Long
    98.   dwVerMajor      As Long
    99.   dwVerMinor      As Long
    100.   dwBuildNumber   As Long
    101.   PlatformID      As Long
    102.   szCSDVersion    As String * 128
    103. End Type
    104. Public Const VER_PLATFORM_WIN32_NT = 2
    105. Public Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" _
    106.   (lpVersionInformation As OSVERSIONINFO) As Long
    107.  
    108.  
    109. Public Function BrowseCallbackProcStr(ByVal hWnd As Long, _
    110.                                       ByVal uMsg As Long, _
    111.                                       ByVal lParam As Long, _
    112.                                       ByVal lpData As Long) As Long
    113.                                        
    114.   'Callback for the Browse STRING method.
    115.  
    116.   'On initialization, set the dialog's
    117.   'pre-selected folder from the pointer
    118.   'to the path allocated as bi.lParam,
    119.   'passed back to the callback as lpData param.
    120.  
    121.    Select Case uMsg
    122.       Case BFFM_INITIALIZED
    123.      
    124.          Call SendMessage(hWnd, BFFM_SETSELECTIONA, _
    125.                           True, ByVal lpData)
    126.                          
    127.          Case Else:
    128.          
    129.    End Select
    130.          
    131. End Function
    132.          
    133.  
    134. Public Function BrowseCallbackProc(ByVal hWnd As Long, _
    135.                                    ByVal uMsg As Long, _
    136.                                    ByVal lParam As Long, _
    137.                                    ByVal lpData As Long) As Long
    138.  
    139.   'Callback for the Browse PIDL method.
    140.  
    141.   'On initialization, set the dialog's
    142.   'pre-selected folder using the pidl
    143.   'set as the bi.lParam, and passed back
    144.   'to the callback as lpData param.
    145.  
    146.    Select Case uMsg
    147.       Case BFFM_INITIALIZED
    148.      
    149.          Call SendMessage(hWnd, BFFM_SETSELECTIONA, _
    150.                           False, ByVal lpData)
    151.                          
    152.          Case Else:
    153.          
    154.    End Select
    155.  
    156. End Function
    157.  
    158.  
    159. Public Function FARPROC(ByVal pfn As Long) As Long
    160.  
    161.   'A dummy procedure that receives and returns
    162.   'the value of the AddressOf operator.
    163.  
    164.   'Obtain and set the address of the callback
    165.   'This workaround is needed as you can't assign
    166.   'AddressOf directly to a member of a user-
    167.   'defined type, but you can assign it to another
    168.   'long and use that (as returned here)
    169.  
    170.   FARPROC = pfn
    171.  
    172. End Function
    173.  
    174. 'Put this in ur form
    175.  
    176.  
    177. Private Sub Command1_Click()
    178.     BrowseForFolderByPath "c:\Windows"
    179. End Sub
    180. Public Function BrowseForFolderByPath(sSelPath As String) As String
    181.  
    182.   Dim BI As BROWSEINFO
    183.   Dim pidl As Long
    184.   Dim lpSelPath As Long
    185.   Dim sPath As String * MAX_PATH
    186.  
    187.   With BI
    188.     .hOwner = Me.hWnd
    189.     .pidlRoot = 0
    190.     .lpszTitle = "Pre-selecting folder using the folder's string."
    191.     .lpfn = FARPROC(AddressOf BrowseCallbackProcStr)
    192.    
    193.     lpSelPath = LocalAlloc(LPTR, Len(sSelPath) + 1)
    194.     CopyMemory ByVal lpSelPath, ByVal sSelPath, Len(sSelPath) + 1
    195.     .lParam = lpSelPath
    196.    
    197.     End With
    198.    
    199.    pidl = SHBrowseForFolder(BI)
    200.    
    201.    If pidl Then
    202.      
    203.       If SHGetPathFromIDList(pidl, sPath) Then
    204.          BrowseForFolderByPath = Left$(sPath, InStr(sPath, vbNullChar) - 1)
    205.       End If
    206.      
    207.       Call CoTaskMemFree(pidl)
    208.    
    209.    End If
    210.    
    211.   Call LocalFree(lpSelPath)
    212.  
    213. End Function
    Hope this helps u
    Last edited by Hack; Jan 10th, 2008 at 08:10 AM. Reason: Added Highlight Tags
    Faisal Muhammed
    Homepage:I Started making it in 1994 ...Still Under Construction
    Using

    Visual Basic 6.0 Enterprise SP5
    Embedded Visual Basic 3.0
    SQL Server 2000
    Windows 2000 Proff
    Delphi 6.0


    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

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