Set ListView Column Widths Using SendMessage API - March 2000

I have been using the ListView control a lot lately. I was using TextWidth to calculate column widths. I don't know where I found it [and I wish I had made some notes to give proper credit] but I discovered that it is possible to use the SendMessage (LVM_SETCOLUMNWIDTH) API to set a column's width using a column number, and a flag that causes the width to be set based upon the width of the data or the width of the column caption. So this becomes the tip-of-the-month.

The Demo Program

I wrote a VB6 demo program that fills a 3 column [report view] ListView with random data. Upon start-up the demo loads the ListView then sets the column widths using the width of the longest data item in each column. You can click the 'Load Random Data' button to clear the list and load a new set of data which causes the column widths to change. Check the 'Use Column Caption Size' box to use the column heading caption to set the column width.

I found that if I used the column heading caption to set the column width and the column caption was shorter than data contained in the column the width is set to the width of the longest data.

In this example the column widths are set using the width of the widest data.

In this example the column widths are set using the width of the column heading.
The 'Use Column Caption Size' is checked.

Made a Class Module

I encapsulated the SendMessage API in a class module. It is called 'clsLVMaxCol'. 

Property/Method Description Sample
bUseHeading If True set the column width using the heading caption. .bUseHeading = True
lColumn The ListView column to be changed. .lColumn = 0 [first column]
lhWnd ListView window handle. .lhWnd = Me.ListView1.hWnd
SetMaxColWidth Method used to set the column widths. See sample code.

All properties can be passed via the call to the 'SetMaxColWidth' method.

Include in Your Code

Include the following class modules in your code:

clsLVMaxCol.cls Sets the Column Width

Problem - LVM_SETCOLUMNWIDTH Special Values Set Incorrect Column Width

2000/02/28 - Brad Martinez reminds me that a problem exists with earlier versions of CommCtl32.dll. The LVSCW_AUTOSIZE and LVSCW_AUTOSIZE_USEHEADER both are used to calculate the width of a column header based on either the header text and item text length. On CommCtl32.dll versions 4.70 and 4.72, the indent is not included in this calculation. From Brad, for a VB work-around, see:

  - http://www.mvps.org/btmtz/listview
  - "How to create a hierarchical ListView that thinks its a TreeView (and displays directories)"
  - Listview.bas
  - ListView_AutosizeFirstColumn procedure

Link to article Q246364 for a full discussion of the problem. By the way, Brad has done some fantastic work with the ListView and TreeView. A visit to his site is time well spend [http://www.mvps.org/btmtz/index.html]. "The site contains a collection of unique 32 bit freeware programs and code samples catering to the intermediate and advanced Visual Basic developer familiar with the Win32 API". 

Other Useful ListView Tips

Tip Month Year
ListView Basics July 2000
Sort Icon in ListView Header February 2002
Show a File's Associated Icon in a ListView April 2003

The Demo Uses modConnectToWebSite

The demo program uses modConnectToWebSite.bas to display this page. For details on the use of that module click here.

Download VB Code

Click here to download the VB 6 Source Code [6k]. 

Previous Tip   Tip Index   Next Tip

Home

Updated 2003/04/05