Solved

CListCtrl width of columns question

Posted on 2001-06-19
4
723 Views
Last Modified: 2013-11-20
Hi,
I have a CListCtrl in a dialog which has 4 columns.
When I created the columns I set each column width, but what I realy want is that each column will have the width of the widest item in the column.
I can see that I can get this effect by double-clicking on the separators of the header of the list, But I need to do this from the code (after finishing inserting all the items).

Can someone help ?
Thanks,
Iris.
0
Comment
Question by:irisz
  • 2
4 Comments
 
LVL 2

Accepted Solution

by:
GloriousRain earned 20 total points
ID: 6209336
Have you tried with CListCtrl::SetColumnWidth

CListCtrl::SetColumnWidth
This method changes the width of a column in report view or list view.

BOOL SetColumnWidth(
int nCol,
int cx );
Parameters
nCol
Specifies the index of the column whose width is to be set. In list view, this parameter must be ?1.
cx
Specifies the new width of the column. Can be either LVSCW_AUTOSIZE, LVSCW_AUTOSIZE_USEHEADER or LVM_SETCOLUMNWIDTH.
Return Value
Nonzero if it is successful; otherwise, it is zero.

0
 
LVL 42

Expert Comment

by:sedgwick
ID: 6209989
i think that u need to check the longest string and use
SetColumn or SetColumnWidth to change the width accordingly to the longest string item in this column
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 6210067
something like that:
     LV_ITEM lvItem;
     int nWidth = 0;

     int nColumnCount = 3;
     int nStringWidth, nItemCount = 10;

     for (int j=0; j<nColumnCount ; j++)
     {
          for (int i=0; i<nItemCount; i++)
          {
               lvItem.iItem = i;
               lvItem.iSubItem = j;
               lvItem.mask = LVIF_TEXT;
               m_list.GetItem(&lvItem);
               nStringWidth = m_list.GetStringWidth(lvItem.pszText);
               nWidth = nStringWidth > nWidth ? nStringWidth : nWidth;
          }

          LV_COLUMN lvc;
          lvc.cx = nWidth;
          m_list.SetColumn(j, &lvc);
     }
     m_list.UpdateData();
0
 

Author Comment

by:irisz
ID: 6214116
works great !
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Decoding 32 bit binary streams 6 43
repeatEnd java challenge 42 84
Folder Comparison 12 54
fizzArray2 challenge 1 81
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question