// JP opened flex table

Click to See Complete Forum and Search --> : FIX: CListCtrl::AutoSizeColumns


Glenn Carr
July 21st, 1998, 05:29 PM
There's a small problem with the AutoSizeColumns routine (http://www.codeguru.com/listview/autosize_col.shtml) if you use it when the style is LVS_LIST instead of LVS_REPORT, in that all the item text labels disappear and only the icons are displayed. In the routine, always change the style to LVS_REPORT before autosizing the columns, and then change it back. Here's my fix...


void CListCtrlEx::AutoSizeColumns( int col )

{

// Call this after your list control is filled

SetRedraw(false);


DWORD dwOldStyle = SetViewStyle ( LVS_REPORT );


int mincol = col < 0 ? 0 : col;

int maxcol = col < 0 ? GetColumnCount()-1 : col;

for (col = mincol; col <= maxcol; col++) {

SetColumnWidth(col,LVSCW_AUTOSIZE);

int wc1 = GetColumnWidth(col);

SetColumnWidth(col,LVSCW_AUTOSIZE_USEHEADER);

int wc2 = GetColumnWidth(col);

int wc = max(MINCOLWIDTH,max(wc1,wc2));

SetColumnWidth(col,wc);

}


SetViewStyle ( dwOldStyle );


SetRedraw(true);

Invalidate();

}

Glenn Carr
July 21st, 1998, 05:35 PM
[Ooops, meant to format it correctly the first time]


There's a small problem with the AutoSizeColumns routine (http://www.codeguru.com/listview/autosize_col.shtml) if you use it when the style is LVS_LIST instead of LVS_REPORT, in that all the item text labels disappear and only the icons are displayed. In the routine, always change the style to LVS_REPORT before autosizing the columns, and then change it back. Here's my fix...




void CListCtrlEx::AutoSizeColumns( int col )

{

// Call this after your list control is filled

SetRedraw(false);




DWORD dwOldStyle = SetViewStyle ( LVS_REPORT );



int mincol = col < 0 ? 0 : col;

int maxcol = col < 0 ? GetColumnCount()-1 : col;

for (col = mincol; col <= maxcol; col++) {

SetColumnWidth(col,LVSCW_AUTOSIZE);

int wc1 = GetColumnWidth(col);

SetColumnWidth(col,LVSCW_AUTOSIZE_USEHEADER);

int wc2 = GetColumnWidth(col);

int wc = max(MINCOLWIDTH,max(wc1,wc2));

SetColumnWidth(col,wc);

}



SetViewStyle ( dwOldStyle );



SetRedraw(true);

Invalidate();

}

//JP added flex table