Adding a column


The CListCtrl::InsertColumn() is used to add a column to the list view control. This function can be used even if the control is not in the report mode. When using this function you should remember to specify the nSubItem argument (or the iSubItem field if you are using the LV_COLUMN structure). The nSubItem is usually equal to the column number. Here is a helper function that makes adding a column easier.

// AddColumn    - adds column after the right most column
// Returns      - the index of the new column if successful or -1 otherwise.
// sColHeading  - the column's heading
// nWidth       - Width of the column, in pixels. If this parameter is -1, 
//                the column width is the same as previous column
// nFormat      - the alignment of the column. Can be LVCFMT_LEFT,
//                LVCFMT_RIGHT, or LVCFMT_CENTER.
int CMyListCtrl::AddColumn(LPCTSTR sColHeading, int nWidth /* = -1*/, int nFormat /* = LVCFMT_LEFT*/)
{
        CHeaderCtrl* pHeader = (CHeaderCtrl*)GetDlgItem(0);
        int nColumnCount = pHeader->GetItemCount();

        if( nWidth == -1 )
        {
                // The line below return zero for ICON views
                //nWidth = GetColumnWidth( nColumnCount - 1 );

                // Get the column width of the previous column from header control
                HD_ITEM hd_item;
                hd_item.mask = HDI_WIDTH;               //indicate that we want the width
                pHeader->GetItem( nColumnCount - 1, &hd_item );
                nWidth = hd_item.cxy;
        }
        return InsertColumn( nColumnCount, sColHeading, nFormat, nWidth, nColumnCount );
}


Comments

  • Adding A Column Heading

    Posted by Legacy on 11/02/2003 12:00am

    Originally posted by: Terri

    What is the function InsertColumn that is used in the return at the end of the code segment?

    Reply
  • Add problems with CListview

    Posted by Legacy on 05/09/2000 12:00am

    Originally posted by: Chris Klecker

    There appears to be a hick-up when changing the number of columns in a CListview window back and forth. When it starts out, at 3 columns it seems to work and look fine. When changing the number of columns to 7 there seems to be an eighth column that is increasing in size. When I revert back to the 3 column view this eighth column, which is now the fourth column, is infinitely large. This also seems to be causing some problems in the control as well as I cannot click on any items in the view and the first column appears to not be painting properly.

    Has anyone else seen this and has there been any documented help in getting around this problem?

    Thanks!

    Chris Klecker

    NOTE:

    I've managed to find the error of my ways. In using the following ListCtrl.DeleteColumn instead of ListCtrl.GetHeaderCtrl()->DeleteItem() it clears the problem.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • With the average hard drive now averaging one terabyte in size, the fallout from the explosion of user-created data has become an overwhelming volume of potential evidence that law-enforcement and corporate investigators spend countless hours examining. Join Us and SANS' Rob Lee for our 45-minute webinar, A Triage and Collection Strategy for Time-Sensitive Investigations, will demonstrate how to: Identify the folders and files that often contain key insights Reduce the time spent sifting through content by …

Most Popular Programming Stories

More for Developers

RSS Feeds