devxlogo

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.

See also  Why ChatGPT Is So Important Today
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist