Set the Width of a ListView Column

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:
data observability

Data Observability Explained

Data is the lifeblood of any successful business, as it is the driving force behind critical decision-making, insight generation, and strategic development. However, due to its intricate nature, ensuring the

Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular