Set the Width of a ListView Column

The ListView Windows control has some additional features that haven’t been exposed in the OCX provided with VB. One feature can shrink or enlarge columns automatically to ensure that data in each column is visible and that no screen space is wasted. Use this function to make an API call:

 Public Declare Function SendMessage Lib "user32" Alias _	"SendMessageA" (ByVal hwnd As Long, ByVal wMsg As _	Long, ByVal wParam As Long, lParam As Any) As LongConst LVM_SETCOLUMNWIDTH = &H1000 + 30Const LVSCW_AUTOSIZE = -1Const LVSCW_AUTOSIZE_USEHEADER = -2Sub AdjustColumnWidth(LV As ListView, AccountForHeaders _	As Boolean)	Dim col As Integer, lParam As Long	If AccountForHeaders Then		lParam = LVSCW_AUTOSIZE_USEHEADER	Else		lParam = LVSCW_AUTOSIZE	End If	' Send the message to all the columns	For col = 0 To LV.ColumnHeaders.Count - 1		SendMessage LV.hwnd, LVM_SETCOLUMNWIDTH, col, ByVal lParam	NextEnd Sub

You can resize all columns, taking the text in column headers into account by passing True as the second argument:

 AdjustColumnWidth ListView1, True

If you pass False as the second argument, the text in column headers is ignored in determining the correct width.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: