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

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

Most Popular Programming Stories

More for Developers

RSS Feeds