Visual Basic Common Control API Routines
SHGetFileInfo: ListView Demo 3 - Adding Sorting Functionality
Third 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
SHGetFileInfo: ListView Demo 2 - Populating the ListView

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 adds a popup menu and the routines necessary to sort and change list views.

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.

To the project form, add a menu as outlined in the illustration. Name the menu items as follows:

Top Level - OptionMenu - mnuOptions
    Item 1 - List View - mnuView(0)
    Item 2 - Detailed Report - mnuView(1)
    Item 3 - separator - zzmnuSep1
    Item 4 - Ascending Order - mnuSortAZ
    Item 5 - Descending Order - mnuSortZA
    Item 6 - separator - zzmnuSep2
    Item 7 - Order By - zzmnuOrderBy
        Item 8 - Name - mnuOrder(0)
        Item 9 - Size - mnuOrder(1)
        Item 10 - Type - mnuOrder(2)
        Item 11 - Created Date - mnuOrder(3)

Once finished, select the top level item and set its visible property to False.

Personal note: In case you're wondering, I like to prefix the name of unused menu items with zz to keep them at the bottom of the code object list, out of the way.

 BAS Module Code
There are no changes to the general declarations area of the project's bas module.

 Form Code
Make the following additions to the form code:

Option Explicit
In the form's general declarations, add:
Dim prevOrder As Integer


In the form code, make the following bolded changes: 
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

    With ListView1
      .SortKey = 0
    End With
      
    UpdateFrequency = 25
    prevOrder = 0
   
End Sub


Add the following new code to the form:
Private Sub ListView1_ColumnClick(ByVal ColumnHeader As ColumnHeader)

    Dim currSortKey As Integer
    
    ListView1.SortKey = ColumnHeader.index - 1
    currSortKey = ListView1.SortKey
    
    ListView1.SortOrder = Abs(Not ListView1.SortOrder = 1)
    ListView1.Sorted = True
    
    mnuOrder(prevOrder).Checked = False
    
    mnuSortAZ.Checked = ListView1.SortOrder = 0
    mnuSortZA.Checked = mnuSortAZ.Checked = False
  
    If currSortKey > -1 Then
      mnuOrder(currSortKey).Checked = True
      prevOrder% = currSortKey
    End If

End Sub


Private Sub ListView1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)

    If Button = 2 Then PopupMenu mnuOptions

End Sub


Private Sub mnuOrder_Click(index As Integer)

    mnuOrder(prevOrder).Checked = False
    mnuOrder(index).Checked = True
    prevOrder = index
    
    ListView1.SortKey = index
    ListView1.SortOrder = 0
    ListView1.Sorted = True
               
    mnuSortAZ.Checked = ListView1.SortOrder = 0
    mnuSortZA.Checked = mnuSortAZ.Checked = False
    
End Sub


Private Sub mnuSortAZ_Click()

    ListView1.SortOrder = 0
    ListView1.Sorted = True
    
    mnuSortAZ.Checked = ListView1.SortOrder = 0
    mnuSortZA.Checked = mnuSortAZ.Checked = False

End Sub


Private Sub mnuSortZA_Click()

    ListView1.SortOrder = 1
    ListView1.Sorted = True
    
    mnuSortAZ.Checked = ListView1.SortOrder = 0
    mnuSortZA.Checked = mnuSortAZ.Checked = False
  
End Sub


Private Sub mnuView_Click(index As Integer)

    mnuView(ListView1.View - 2).Checked = False
    mnuView(index).Checked = True
    
    ListView1.View = index + 2
    ListView1.Sorted = True
     
End Sub
 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. In addition, the listview should populate with all the files in the selected directory matching the selected file type.

By right-clicking over the listview control, you can select from the popup menu. As well, the clicking the column headers will also sort the items. Note however that the numeric sorting of the file size is inaccurate; a ListView normally calls a special routine via callbacks to sort non-alpha items. To overcome this limitation, a callback must be used (VB5 only).  This demo does not cover implementing the callback; see dd.

Move to SHGetFileInfo: ListView Demo 4 - Adding the Associated Icons

 
 

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