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

  • IBM Worklight is a mobile application development platform that lets you extend your business to mobile devices. It is designed to provide an open, comprehensive platform to build, run and manage HTML5, hybrid and native mobile apps.

  • On-demand Event Event Date: October 23, 2014 Despite the current "virtualize everything" mentality, there are advantages to utilizing physical hardware for certain tasks. This is especially true for backups. In many cases, it is clearly in an organization's best interest to make use of physical, purpose-built backup appliances rather than relying on virtual backup software (VBA - Virtual Backup Appliances). Join us for this webcast to learn why physical appliances are preferable to virtual backup appliances, …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds