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

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

  • When it comes to desktops – physical or virtual – it's all about the applications. Cloud-hosted virtual desktops are growing fast because you get local data center-class security and 24x7 access with the complete personalization and flexibility of your own desktop. Organizations make five common mistakes when it comes to planning and implementing their application management strategy. This eBook tells you what they are and how to avoid them, and offers real-life case studies on customers who didn't let …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds