listview2

 

Autosizing ListView Headers


Autosizing ListView Headers

If the user resized the headers at runtime, and caused some headers to be
out of the listview visible range, you can use the code below to resize
the headers in way that all of them will be visible.


Preparations

Add 1 ListView Control and 1 Command Button to your form.
Set the ListView View property to 3 - lvwReport.


Module Code

Declare Function SendMessage Lib "user32.dll" _
Alias "SendMessageA" (ByVal hWnd As Long, _
ByVal Msg As Long, ByVal wParam As Long, _
ByVal lParam As Long) As Long
Public Const LVM_FIRST = &H1000
Public Const LVM_SETCOLUMNWIDTH = (LVM_FIRST + 30)
Public Const LVSCW_AUTOSIZE = -1
Public Const LVSCW_AUTOSIZE_USEHEADER = -2

Form Code

Private Sub Form_Load()
'the code below add 3 headers to the ListView
With ListView1
.ColumnHeaders.Add , , "Header 1"
.ColumnHeaders.Add , , "Header 2"
.ColumnHeaders.Add , , "Header 3"
End With
End Sub


Private Sub Command1_Click()
Dim Column As Long
Dim Counter As Long
Counter = 0
For Column = Counter To ListView1.ColumnHeaders.Count - 2
SendMessage ListView1.hWnd, LVM_SETCOLUMNWIDTH, _
Column, LVSCW_AUTOSIZE_USEHEADER
Next
End Sub