Solved

CListCtrl width of columns question

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

760 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

17 Experts available now in Live!

Get 1:1 Help Now