Auto resizing column widths in a CListCtrl
Asked Answered
S

3

13

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?

Sheridansherie answered 9/4, 2010 at 7:53 Comment(0)
C
15

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.

Chainsmoke answered 9/4, 2010 at 19:45 Comment(4)
Hey, I tried it and it seems to work well :) :) Thanks a lot for the reply dwo :DSheridansherie
A small question dwo, I have never heard about HeaderControl. What exactly is it??? Thanks a lot!!!Sheridansherie
The header control is the upper part of the list control with the column titles in it.Chainsmoke
Using LVSCW_AUTOSIZE will resize based on the contents, instead of the header text.Alvord
E
1

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.

Elyseelysee answered 9/4, 2010 at 10:19 Comment(0)
G
1

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.

Gabrielegabriell answered 9/4, 2010 at 13:9 Comment(1)
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.Sheridansherie

© 2022 - 2024 — McMap. All rights reserved.