Editing Labels

When you create a list view control, you can enable label editing by specifying the LVS_EDITLABELS style, which lets a user edit an item's text label in place. The user begins by clicking the label of an item that has the focus. The list view control notifies the parent window with an LVN_BEGINLABELEDIT notification. If you do not want to allow label editing on certain items, you can return a nonzero value to disallow it. To limit the amount of text the user can enter, the application gets the handle to the edit window through the LVM_GETEDITCONTROL message (or the MFC GetEditControl member function) and sends the EM_SETLIMITTEXT message to the edit control (or uses the MFC LimitText member function of the CEdit class), specifying the maximum number of characters that can be entered.

Once editing is completed, the list view control sends its parent window an LVN_ENDLABELEDIT notification. The lParam parameter is the address of an LV_DISPINFO structure identifying the item and specifying the edited text. The parent window is responsible for updating the item's label. If editing is canceled, the iItem member is -1. Be alert to the possibility of getting a valid index to an item but getting a NULL pointer back for the text. This happens if the user chooses an item and immediately presses the Enter key.

The following code from the MFCLIST sample shows how to support label editing in a list view control:

case LVN_BEGINLABELEDIT:
{
CEdit *pEdit;
// Get the handle to the edit control.
pEdit = m_ListCtl.GetEditControl ();
// Limit the amount of text that the user can enter.
pEdit->LimitText (20);
}
break;

case LVN_ENDLABELEDIT:
// If label editing wasn't canceled and the
// text buffer is non-NULL...
if ((pLvdi->item.iItem != -1) && (pLvdi->item.pszText != NULL))
// Save the new label information.
lstrcpy (pHouse->szAddress, pLvdi->item.pszText);
break;

That's all there is to it. The LISTVIEW and MFCLIST samples should be enough to get you started if you plan to include list view controls in your application.