
Column width of List view
Quote:
> I want to know how to automatic set the column width of List View
according
> the data length display
You need to send a message to the ListView control...specifically, the
LVM_SETCOLUMNWIDTH message, repeated for each column. Here's sample code.
Private Const LVM_FIRST As Long
= &H1000
Private Const LVM_SETCOLUMNWIDTH As Long = LVM_FIRST + 30
Private Const LVSCW_AUTOSIZE As Long = -1
Private Const LVSCW_AUTOSIZE_USEHEADER As Long = -2
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
#If VB6 Then
Public Sub AutoSizeLVColumns(oLV As MSComctlLib.ListView, Optional ByVal
bUseHeader As Boolean = True)
#Else
Public Sub AutoSizeLVColumns(oLV As ComctlLib.ListView, Optional ByVal
bUseHeader As Boolean = True)
#End If
Dim lColumnIndex As Long
For lColumnIndex = 0 To oLV.ColumnHeaders.Count - 1
If bUseHeader Then
Call SendMessage(oLV.hwnd, LVM_SETCOLUMNWIDTH, lColumnIndex,
ByVal LVSCW_AUTOSIZE_USEHEADER)
Else
Call SendMessage(oLV.hwnd, LVM_SETCOLUMNWIDTH, lColumnIndex,
ByVal LVSCW_AUTOSIZE)
End If
Next
End Sub
If you specify True for bUseHeader, the column will size based on the text
in the ColumnHeader; otherwise, it will size the columns based on the
longest text within that column.
Mike