Autosize Limits on ListView Column Sizes

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?
 
A

Armin Zingler

Kevin Westhead said:
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:

VB5 is dead. This is a group about VB.Net (see group name).
microsoft.public.vb.* deals with older verions.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
K

Kevin Westhead

Armin Zingler said:
VB5 is dead. This is a group about VB.Net (see group name).
microsoft.public.vb.* deals with older verions.

You're right (about the newsgroup), I absent mindedly posted to the wrong
group - sorry.

Having said that, a quick test with a WinForms list-view shows that the same
problem occurs, so it's most likely a problem with the underlying common
control rather than any language-specific issue.
 
P

Peter Huang

Hi Kevin,

I think you may try to take a look at the link below, is that what are you
looking for? If you still have any concern on this issue, please feel free
to post here.

SYMPTOMS
When you add long strings to a ListView control, all of the string is added
to the items collection, but the control can only display the first 259
characters of each item.

PRB: ListView Control Can Only Display 259 Characters per Column (321104)
http://support.microsoft.com/default.aspx?scid=KB;EN-US;321104

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
K

Kevin Westhead

"Peter Huang" said:
SYMPTOMS
When you add long strings to a ListView control, all of the string is added
to the items collection, but the control can only display the first 259
characters of each item.

PRB: ListView Control Can Only Display 259 Characters per Column (321104)
http://support.microsoft.com/default.aspx?scid=KB;EN-US;321104


Thanks Peter.

Can you explain why this is by design, i.e. why does a limit exist for
displaying an item's text but there is no limit for the actual item text
stored with the item? Is it performance related? I take it then that the
autosizing algorithm uses the displayed string then?
 
P

Peter Huang

Hi Kevin,

Thank you for your response.
Why the length of the text of listview item is limited to 259 character is
because that the listview is design for display collection of objects like
files and not for general purpose control. So it is similar to the filename
length limitation in the windows file system's MAX_PATH.

Hope this helps.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
K

Kevin Westhead

"Peter Huang" said:
Why the length of the text of listview item is limited to 259 character is
because that the listview is design for display collection of objects like
files and not for general purpose control. So it is similar to the filename
length limitation in the windows file system's MAX_PATH.


Okay, thanks. It seems like it would still be a good idea to use trailing
ellipsis when drawing the text for items that have text > 259 characters.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top