K
Kevin Westhead
Can anyone confirm whether or not there are any limits imposed on the widths
of autosized columns in a list-view. I've found that the autosizing appears
to have an upper limit, i.e. it will not expand beyond some (unknown)
maximum length. This applies to user-autosizing (double-clicking the
divider) and code-autosizing using LVSCW_AUTOSIZE.
For example, create a new project and place a ListView (VB5 version) and
CommandButton on a form and insert the following code:
Const LVM_SETCOLUMNWIDTH As Long = &H101E
Const LVM_SETEXTENDEDLISTVIEWSTYLE As Long = &H1036
Const LVS_EX_LABELTIP As Long = &H4000
Const LVSCW_AUTOSIZE As Long = &HFFFFFFFF
Const LVSCW_AUTOSIZE_USEHEADER As Long = &HFFFFFFFE
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 Command1_Click()
Dim i As Long
Call ListView1.ListItems.Clear
For i = 0 To 100
With ListView1.ListItems.Add(, , String$(300, AscW(CStr(i))))
.SubItems(1) = String$(300, AscW(CStr(i)))
.SubItems(2) = String$(300, AscW(CStr(i)))
End With
Next
Call SendMessage(ListView1.hWnd, LVM_SETCOLUMNWIDTH, 1, ByVal
LVSCW_AUTOSIZE_USEHEADER)
Call SendMessage(ListView1.hWnd, LVM_SETCOLUMNWIDTH, 2, ByVal
LVSCW_AUTOSIZE)
End Sub
Private Sub Form_Load()
ListView1.View = lvwReport
Call ListView1.ColumnHeaders.Add(, , "Column 1")
Call ListView1.ColumnHeaders.Add(, , "Column 2")
Call ListView1.ColumnHeaders.Add(, , "Column 3")
Call SendMessage( _
ListView1.hWnd, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_LABELTIP, ByVal
LVS_EX_LABELTIP)
End Sub
I've noticed the following:
1. "Column 3" does not completely autosize, it stops short of the full range
of characters and also does not apply trailing ellipsis. The limit seems to
be 260 chars (259 chars + null terminator), however this only applies to
"drawing" the text since the tooltip indicates that the full text is
correctly stored in the list-view.
2. "Column 2" does not autosize to the header, LVSCW_AUTOSIZE_USEHEADER
seems to be working like LVSCW_AUTOSIZE here (note that it is not the last
column, hence it should really autosize to the column header).
3. Double-clicking the divider to the right of "Column 1" produces the same
results for "Column 1" as 1. does for "Column 3."
Is this behaviour intentional? I could understand the limits to autosizing
columns based on the fact that true autosizing could get crazy for columns
containing large amounts of text, however there's nothing documented in
MSDN. The clipped text seems like a drawing bug to me, maybe related to
using a MAX_PATH sized buffer for drawing?
of autosized columns in a list-view. I've found that the autosizing appears
to have an upper limit, i.e. it will not expand beyond some (unknown)
maximum length. This applies to user-autosizing (double-clicking the
divider) and code-autosizing using LVSCW_AUTOSIZE.
For example, create a new project and place a ListView (VB5 version) and
CommandButton on a form and insert the following code:
Const LVM_SETCOLUMNWIDTH As Long = &H101E
Const LVM_SETEXTENDEDLISTVIEWSTYLE As Long = &H1036
Const LVS_EX_LABELTIP As Long = &H4000
Const LVSCW_AUTOSIZE As Long = &HFFFFFFFF
Const LVSCW_AUTOSIZE_USEHEADER As Long = &HFFFFFFFE
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 Command1_Click()
Dim i As Long
Call ListView1.ListItems.Clear
For i = 0 To 100
With ListView1.ListItems.Add(, , String$(300, AscW(CStr(i))))
.SubItems(1) = String$(300, AscW(CStr(i)))
.SubItems(2) = String$(300, AscW(CStr(i)))
End With
Next
Call SendMessage(ListView1.hWnd, LVM_SETCOLUMNWIDTH, 1, ByVal
LVSCW_AUTOSIZE_USEHEADER)
Call SendMessage(ListView1.hWnd, LVM_SETCOLUMNWIDTH, 2, ByVal
LVSCW_AUTOSIZE)
End Sub
Private Sub Form_Load()
ListView1.View = lvwReport
Call ListView1.ColumnHeaders.Add(, , "Column 1")
Call ListView1.ColumnHeaders.Add(, , "Column 2")
Call ListView1.ColumnHeaders.Add(, , "Column 3")
Call SendMessage( _
ListView1.hWnd, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_LABELTIP, ByVal
LVS_EX_LABELTIP)
End Sub
I've noticed the following:
1. "Column 3" does not completely autosize, it stops short of the full range
of characters and also does not apply trailing ellipsis. The limit seems to
be 260 chars (259 chars + null terminator), however this only applies to
"drawing" the text since the tooltip indicates that the full text is
correctly stored in the list-view.
2. "Column 2" does not autosize to the header, LVSCW_AUTOSIZE_USEHEADER
seems to be working like LVSCW_AUTOSIZE here (note that it is not the last
column, hence it should really autosize to the column header).
3. Double-clicking the divider to the right of "Column 1" produces the same
results for "Column 1" as 1. does for "Column 3."
Is this behaviour intentional? I could understand the limits to autosizing
columns based on the fact that true autosizing could get crazy for columns
containing large amounts of text, however there's nothing documented in
MSDN. The clipped text seems like a drawing bug to me, maybe related to
using a MAX_PATH sized buffer for drawing?