Sort Icon in ListView Header - February 2002

A year or two ago I wanted to add a 'sort icon' to a ListView header. The icon 'jumps' from column to column and shows an up or down arrow to indicate the column's sort direction. I have seen requests on the news groups for this function so I decided to write a demo program and make this the February 2002 Tip-of-the-Month.

The Demo Program

I created a VB 6 demo program using a ListView control. The ListView contains 4 columns, 2 text and 2 numbers. The demo generates 25 rows of random text and numbers. Click a column header to change the sort order. Notice that an up or down arrow is shown 'right' of the caption. The VB 6 demo program:


ListView sorted on the second column.

Key Design Elements

Tie the Image Control to the ListView - You first need to 'connect' an image control that contains the 'up and down' arrows. The image control properties:


Two 7x8 custom icons [provided in the download file] are added to the image control.
You could copy and paste the image control contained in this demo to a form in your application.

In the Code - Add the following code in your program to tie the image control to the ListView headers:

Calling the Routine - Call subroutine 'ShowListViewColumnHeaderSortIcon', for example:


Usually done as the last instruction in the ListView1_ColumnClick event.

Add To Your Application

Add the following module to your application if you want to add 'Sort Icon' support for your ListView controls:

Module Description
modShowListViewColumnHeaderSortIcon.bas Add support to your application for this tip.

The Demo Program Uses This Module

Module - Click for More Info Description Used in the Demo To
modConnectToWebSite.bas Connect to a web site Show this info when Help/Info is
selected from the demo.

Other Useful ListView Tips

Tip Month Year
Set ListView Column Widths Using SendMessage API March 2000
ListView Basics July 2000
Show a File's Associated Icon in a ListView April 2003

Download VB 6 Source Code

Click here to download the VB 6 source code [9k]

Previous Tip   Tip Index   Next Tip    First Tip

Home

Updated 2003/04/05