How can I make a CListCtrl
to resize the width of its columns automatically? Usually, when an item in the list gets too long, the back end disappears from view and the user manually has to resize the width of the corresponding column.
Is there any way to do this by code?
3 Answers
Resizing the columns automatically is easy:
for(int i = 0;i < pListCtrl->GetHeaderCtrl()->GetItemCount();++i)
pListCtrl->SetColumnWidth(i,LVSCW_AUTOSIZE_USEHEADER);
This will optimize the columns.

- 3,576
- 2
- 22
- 39
-
Hey, I tried it and it seems to work well :) :) Thanks a lot for the reply dwo :D – Isuru Apr 10 '10 at 04:19
-
A small question dwo, I have never heard about HeaderControl. What exactly is it??? Thanks a lot!!! – Isuru Apr 10 '10 at 04:35
-
The header control is the upper part of the list control with the column titles in it. – dwo Apr 10 '10 at 20:02
-
10Using `LVSCW_AUTOSIZE` will resize based on the contents, instead of the header text. – Kevin Smyth Sep 27 '12 at 16:40
Do you have the "No Scroll" option on? By default ("No Scroll" option off), if an item got too long a scroll bar would appear.

- 4,542
- 4
- 42
- 45
I assume you mean a list control in report mode? Unfortunately there is no way to automatically resize columns. What you can do (what I do in some places) is calculate the width of columns as you enter items, then handle WM_SIZE and resize the columns. However this causes changes that the user made to be lost, so you may need a better algorithm like tracking if the user made any changes, if he did don't resize. It's not very nice from a UX perspective, I only use it in a select amount of circumstances where the behavior makes sense in the context of the rest of the UI.

- 19,338
- 6
- 61
- 90
-
Yes that is exactly what I meant. Thanks for the reply, if I cant get a suitable algo, I'll just handle OnSize() for resizing manually and ask the reader to resize the window. – Isuru Apr 10 '10 at 04:18