tags:

views:

325

answers:

2

In MFC, I can edit the text of items in the list control but only for the first column by setting the Edit Labels to true. Now when I click the first column item to change its text, I'm able to change its text but when I hit Enter, its text isn't updated, why and how do I edit text for other columns?

A: 

For the first column:

  • create the list with the LVS_EDITLABELS style
  • set a dialog control on your list if it does not have one, eg SetDlgCtrlID( ID_EDITLABEL );
  • you might need some code for tracking the current selection
  • create the edit in a messagehandler reacting on a click/doubleclick or other user input (seems you have that covered already), best to put this in the parent class.
  • add a handler for the edit end in the parent class

    ON_NOTIFY( LVN_ENDLABELEDIT, ID_EDITLABEL, OnEndEdit )
    
    
    void MyParentClass::OnEndEdit( NMHDR* pNMHDR, LRESULT* pResult )
    {
      NMLVDISPINFO* pLVDI = reinterpret_cast< NMLVDISPINFO* >( pNMHDR );
      if( pLVDI->item.pszText )
        m_List.SetItemText( m_iCurrentSelection, 0, pLVDI->item.pszText );
      *pResult = 0;
    }
    

For other columns: I haven't tried it yet, but it should not be too hard, as you can lookup in the MFC source code how they do it. Note that the code above is tested with a CMFCListCtrl from the latest feature pack, though I assume a plain CListCtrl behaves the same.

stijn
A: 

Unfortunately it isn't possible to utilize LVS_EDITLABELS and LVN_ENDLABELEDIT for editing other columns than the first.

For a workaround see the XListCtrl article on CodeProject for further information, it dynamically creates an edit control when needed.

dalle