Show a File's Associated Icon in a ListView - April 2003

I have noted requests in news groups asking how to display a file's associated icon in a ListView. Using work done by Brad Martinez [http://www.mvps.org/btmtz/listview/], I developed code to use API SHGetFileInfo to retrieve the icon and API ImageList_Draw to load the icon into an ImageList. I recently revisited the code and decided to make it the April 2003 tip-of-the-month.

2003/11/30 Updated - Add a prefix to the item's key. Needed in case the file name is entirely numeric. See below.

The Demo Program

I created a VB6 demo program that upon start-up displays the Windows folder:

WinNT Folder Sorted by Size
WinNT folder, sorted by file size.
Click a column heading to sort that column.
Click again to change from ascending to descending sort order and back.

Browse - Click the 'Browse for Folder' button to select another folder, for example:

Browse for Folder
Select folder 'Jasper' photos.

Cycle Views - Click the 'Cycle ListView Views' to cycle through the four ListView 'views', for example:

Jasper Photos
One of the possible four ListView views.

Using the Tip

To use this tip in your application first add one ListView then add two ImageList and two PictureBox controls to your form. The easiest way to add the ImageList and PictureBox controls to your form is to copy [Ctrl-C] the four controls from this tip and paste [Ctrl-V] them onto your form. The copied controls:

Copy Controls
Copy these four controls to your form.

Subroutines Description
LV_ClearImageListsAndRelinkToListView(list As ListView, imgLarge As ImageList, imgSmall As ImageList) Clear the ImageLists and relink the ListView. Do this before each 'redisplay' of the ListView. Clears all but one of the icons from the ImageList.
  list The form's ListView control
  imgLarge, imgSmall The image list controls copied to the form. See above.
Public Sub LV_AddFileToListView(sPath As String, _
 sFileName As String, _
 sKeyPrefix as String, _
 list As ListView, iTypeColumn As Integer, _
 imgLarge As ImageList, imgSmall As ImageList, _
 picLarge As PictureBox, picSmall As PictureBox)
Add a file and its associated icon to the ListView. Do this for each file to be added to the ListView.
  sPath The file's fully qualified path.
  sFileName File name to be displayed in the ListView. See note in next paragraph.
  sKeyPrefix Any string character prefixed to sFileName and used as the item's key.
  list Add the item to this ListView.
  iTypeColumn The SubItem column in which the file description is to be shown. Enter -1 [minus one] to skip.
  imgLarge, imgSmall The image lists into which the form's icons are placed.
  picLarge, picSmall Draw the icons in these picture boxes.

File Names Consisting Entirely of Numbers

2003/08/18 - The demo uses the file name as the listview key. I received the following e-mail from Timothy McGill:

You saved me hours of coding with "Show a File's Associated Icon in a ListView" tip. Top notch.

I did notice that a Listview Item's key cannot be a string consisting solely of numerics, e.g. "13653". Thus, if you have any files named as such, you'll get an invalid key error when invoking the Add method in subroutine LV_AddFileToListView. I simply pre-pended the letter 'A' to every key and I use a pre-pended 'A' in every reference context.

Thanks,
Timothy McGill

Modules Used in the Demo

Module - Click for More Info Description Used in the Demo To
modExtractIcon.bas Contains the subroutines described above. The subject of this tip. Include it in your application.
cBrowseFolder.cls
mBrowseFolder.bas
Browse for a folder. Browse for a folder. Subject of another tip.
modConnectToWebSite.bas Connect to a web site Show this info when Help/Info is selected from the demo.
modInfoAbout.bas Info and about help. Provide help via the web and 'about' info.

Other Useful ListView Tips

Tip Month Year
Set ListView Column Widths Using SendMessage API March 2000
ListView Basics July 2000
Sort Icon in ListView Header February 2002

Download VB 6 Source Code

Click here to download the VB 6 source code [14k]. If you downloaded this tip before 2003/11/30 you may want to download it again. See above.

Previous Tip   Tip Index   Next Tip   First Tip

Home

Updated 2011/10/06