?
Solved

CListCtrl width of columns question

Posted on 2001-06-19
4
Medium Priority
?
738 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 2

Accepted Solution

by:
GloriousRain earned 80 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

764 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