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
Solved

CListCtrl width of columns question

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

837 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