Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _ (ByVal hwnd As Long, _ ByVal wMsg As Long, _ ByVal wParam As Long, _ lParam As Any) As Long
Private Sub AutosizeColumns(ByVal TargetListView As ListView)
Const SET_COLUMN_WIDTH As Long = 4126 Const AUTOSIZE_USEHEADER As Long = -2
Dim lngColumn As Long
For lngColumn = 0 To (TargetListView.ColumnHeaders.Count - 1)
Call SendMessage(TargetListView.hwnd, _ SET_COLUMN_WIDTH, _ lngColumn, _ ByVal AUTOSIZE_USEHEADER)
Next lngColumn
End Sub
Private Sub Form_Load() Call LoadListView
Call AutosizeColumns(ListView1) End Sub Sub LoadListView() Dim i As Integer Dim itmX As ListItem
ListView1.ListItems.Clear ListView1.ColumnHeaders.Clear
With ListView1 .View = lvwReport
.ColumnHeaders.Add , , "MyHeaderrrrrrrrrr1" .ColumnHeaders.Add , , "MyHeaderrrrrrrrrr2" .ColumnHeaders.Add , , "MyHeaderrrrrrrrrr3"
For i = 1 To 10 Set itmX = .ListItems.Add With itmX .Text = "long itemmmmmmmmmmmmmm" & i .SubItems(1) = "long sub itemmmmmmmmmmmmmm1-" & i .SubItems(2) = "long sub itemmmmmmmmmmmmmm2-" & i End With Next End With End Sub |