Results 1 to 2 of 2

Thread: Select a Folder in Visual Basic

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2005
    Posts
    1

    Select a Folder in Visual Basic

    To select a file I use CommonDialog.

    But what i use for select a folder?

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Select a Folder in Visual Basic

    VB Code:
    1. 'in a module
    2. Option Explicit
    3.  
    4. Public Type BrowseInfo
    5.     hWndOwner As Long
    6.     pIDLRoot As Long
    7.     pszDisplayName As Long
    8.     lpszTitle As Long
    9.     ulFlags As Long
    10.     lpfnCallback As Long
    11.     lParam As Long
    12.     iImage As Long
    13. End Type
    14.  
    15. Public Const BIF_RETURNONLYFSDIRS = 1
    16. Public Const BFFM_INITIALIZED = 1
    17. Public Const MAX_PATH = 260
    18.  
    19. Public Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
    20. Public Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
    21. Public Declare Function MoveWindow Lib "User32" (ByVal hWnd As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
    22. Public Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
    23. Public Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long
    24.  
    25. Public Function BrowseForFolder(hWndOwner As Long, sPrompt As String) As String
    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.     With udtBI
    33.         .hWndOwner = hWndOwner
    34.         .lpszTitle = lstrcat(sPrompt, "")
    35.         .ulFlags = BIF_RETURNONLYFSDIRS
    36.         .lpfnCallback = PassBackAddress(AddressOf BrowseCallBackProc)
    37.     End With
    38.  
    39.     lpIDList = SHBrowseForFolder(udtBI)
    40.     If lpIDList Then
    41.         sPath = String$(MAX_PATH, 0)
    42.         lResult = SHGetPathFromIDList(lpIDList, sPath)
    43.         Call CoTaskMemFree(lpIDList)
    44.         iNull = InStr(sPath, vbNullChar)
    45.         If iNull Then
    46.             sPath = Left$(sPath, iNull - 1)
    47.         End If
    48.     End If
    49.  
    50.     BrowseForFolder = sPath
    51.  
    52. End Function
    53.  
    54. Public Function BrowseCallBackProc(ByVal BrowseHWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lpData As Long) As Long
    55.     Dim lReturn As Long
    56.     If uMsg = BFFM_INITIALIZED Then
    57.         lReturn = MoveWindow(BrowseHWnd, 50, 50, 425, 400, 1)
    58.     End If
    59.     BrowseCallBackProc = 0
    60. End Function
    61.  
    62. Public Function PassBackAddress(ByVal lPointer As Long) As Long
    63.     PassBackAddress = lPointer
    64. End Function
    65.  
    66. 'on a form
    67. Private Sub Command1_Click()
    68. Dim sMyFolder As String
    69. sMyFolder = BrowseForFolder(Form1.hWnd, "Select A Folder And Click OK")
    70. Text1.Text = sMyFolder
    71. End Sub

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