Visual Basic Common Control API Routines
SHGetFileInfo: ListView Demo 1 - Obtaining the File Path
First of four pages to create a ListView application to retrieve files from a specified folder.
     
Posted:   Sunday March 1, 1997
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB4-32, Windows 95
OS restrictions:   None
Author:   VBnet - Randy Birch
     
Related:   SHGetFileInfo: ListView Demo 1 - Obtaining the File Path
SHGetFileInfo: ListView Demo 2 - Populating the ListView
SHGetFileInfo: ListView Demo 3 - Adding Sorting Functionality
SHGetFileInfo: ListView Demo 4 - Adding the Associated Icons
     
 Prerequisites
This method is intended for Visual Basic 5 or Visual Basic 6 where the Common Control library used is the MSComCtl 5 version (comctl32.ocx). Because the VB6-specific mscomctl.ocx (Common Controls 6) is a complete implementation of comctl32.dll and not reliant on the version of comctl32.dll installed, this routine may not work when applied to a listview created from the VB6-specific mscomctl.ocx.

Enhanced Comctl32 functionality is only available to users with comctl32.dll version 4.70 or greater installed. This dll is typically installed with IE3.x or greater.


This page details the code required to construct the base form, and the code necessary to retrieve the user's selection using the Windows Browse dialog.

When the demo is completed, the final app will retrieve the users selection and populate the listview with selected files from that folder, complete with associated icons, file name, file type, file size and created date.

Begin a new project, and add the Common Controls OCX. Create Form1 and as shown in the illustration, add a combo box (Combo1) set to Style 2, and three command buttons in a control array - cmdSelect(0) "Select Folder", cmdSelect(1) "Rescan" and cmdSelect(2) "Exit"). Finally, add Label1 with the caption "Target Folder", and an empty label named DisplayName with AutoSize set to true.
 BAS Module Code
Place the following code into the general declarations area of a bas module:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Const BIF_RETURNONLYFSDIRS = &H1
Public Const MAX_PATH As Long = 260

Public Type SHITEMID
  cb      As Long
  abID    As Byte
End Type

Public Type ITEMIDLIST
  mkid    As SHITEMID
End Type

Public Type BROWSEINFO
  hOwner          As Long
  pidlRoot        As Long
  pszDisplayName  As String
  lpszTitle       As String
  ulFlags         As Long
  lpfn            As Long
  lParam          As Long
  iImage          As Long
End Type

Public Declare Function SHGetPathFromIDList Lib "shell32" _
   Alias "SHGetPathFromIDListA" _
  (ByVal pidl As Long, _
   ByVal pszPath As String) As Long

Public Declare Function SHBrowseForFolder Lib "shell32" _
   Alias "SHBrowseForFolderA" _
  (lpBrowseInfo As BROWSEINFO) As Long

Public Declare Sub CoTaskMemFree Lib "ole32" 
   (ByVal pv As Long)
 Form Code
To the form, add the following code:

Option Explicit

Dim fPath As String

Private Sub Form_Load()

    Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2
    
    With Combo1
        .AddItem "All Files and Folders (*.*)"
        .AddItem "Applications (*.exe)"
        .AddItem "Device Drivers (*.drv)"
        .AddItem "Documents (*.doc)"
        .AddItem "Dynamic Link Libraries (*.dll)"
        .AddItem "Rich Text Format Documents (*.rtf)"
        .AddItem "System Files (*.sys)"
        .AddItem "Visual Basic Modules (*.bas)"
        .AddItem "Visual Basic Forms (*.frm)"
        .AddItem "Visual Basic 3 Projects (*.mak)"
        .AddItem "Visual Basic 4 Projects (*.vbp)"
        .AddItem "Postscript Printer Font Metrics (*.pfm)"
        .AddItem "Text Files (*.txt)"
        .AddItem "True Type Fonts (*.ttf)"
        .AddItem "Windows Help Files (*.hlp)"
        .AddItem "Windows Shortcuts (*.lnk)"
        .ListIndex = 0
    End With

End Sub


Private Sub cmdSelect_Click(index As Integer)

    Dim fExt As String
    
    Select Case index
      
        Case 0: fPath = vbGetBrowseDirectory()
                fExt = vbGetComboFileSpec()
                
                If Len(fPath) > 0 And Len(fExt) > 0 Then
                    DisplayName = fPath & fExt
                End If
        
        Case 2: Unload Me
    
    End Select
  
End Sub


Private Sub Combo1_Click()

    cmdSelect(1).Enabled = Len(fPath) > 0

End Sub


Private Function ValidateDir(tmpPath As String) As String

    If Right$(tmpPath, 1) = "\" Then
       ValidateDir = tmpPath
    Else
       ValidateDir = tmpPath & "\"
    End If

End Function


Private Function vbGetBrowseDirectory() As String

    Dim bi As BROWSEINFO
    Dim IDL As ITEMIDLIST
    Dim pidl As Long
    Dim tmpPath As String
    Dim pos As Integer
    
    bi.hOwner = Form1.hWnd
    bi.pidlRoot = 0&
    bi.lpszTitle = "Select the search directory for " & vbGetComboTargetType$() & "."
    bi.ulFlags = BIF_RETURNONLYFSDIRS
    
   'get the folder
    pidl = SHBrowseForFolder(bi)
    
    tmpPath = Space$(MAX_PATH)
      
    If SHGetPathFromIDList(ByVal pidl, ByVal tmpPath) Then
       pos = InStr(tmpPath, Chr$(0))
       tmpPath = Left(tmpPath, pos - 1)
       vbGetBrowseDirectory = ValidateDir(tmpPath)
    Else 
       vbGetBrowseDirectory = ""
    End If

    Call CoTaskMemFree(pidl)

End Function


Private Function vbGetComboFileSpec() As String

    Dim pos As Integer
    Dim lpos As Integer
    Dim item As String
    
    item = Combo1.List(Combo1.ListIndex)
    pos = InStr(item, "(") + 1
    lpos = InStr(item, ")") - pos
    
    vbGetComboFileSpec = Mid$(item, pos, lpos)

End Function


Private Function vbGetComboTargetType() As String

    Dim pos As Integer
    Dim item As String
    
    item = Combo1.List(Combo1.ListIndex)
    pos = InStr(item, "(") - 2
    
    vbGetComboTargetType = Left$(item, pos)

End Function
 Comments
Run the project, and click the Select Folder button. Browse to any folder, and hit OK. The folder selected and the file type from the combo box should be returned in the DisplayName label as a fully-qualified path and filespec.

Move to SHGetFileInfo: ListView Demo 2 - Populating the ListView.

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter