Solved

CListCtrl width of columns question

Posted on 2001-06-19
4
718 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Question regarding Copy/Paste 16 91
iSeries FTP Exit Program 8 123
either24  challenge 19 90
Window placement 17 75
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now