// in class header
// in class implementation
// void MyClass::Rename()
nItem = m_MyListCtrl.GetNextItem(-1, LVNI_SELECTED);
m_MyListCtrl.SendMessage(LVN_BEGINLABELEDIT, 0, 0);
m_MyListCtrl.EditLabel(nItem); // how to make edit permanent ?
m_MyListCtrl.SendMessage(LVN_ENDLABELEDIT, 0, 0);
There are no errors encountered. The label appears to edit properly, but when leaving the item, the old label reappears.
Last edited by Mike Pliam; December 24th, 2011 at 02:47 AM.
You'll notice in the many comments accompanying this article that the method proposed, which is devised using CListView, doesnt work with CListCtrl.
My app is based on a CView with a pane (CWorkspaceWnd) derived from CDockablePane containing a CListCtrl member that does all the heavy lifing. It works fine except that the item edits disappear as noted by myself and many others.
I have tried to add Reg Anderson's ListEditView.h and ListEditView.cpp to my test app, but it will not compile (VS 2010), allegedly not recognizing CListVew as a class. Consequently, a connot substitute the CWorkdspaceWnd member, CListCtrl m_wndWorkspaceList, with CListEditView m_m_wndWorkspaceList. So that approach appears to be a dead end.
My next effort was to add the CLVEdit class of Reg Anderson and implement
identical to the method used by Anderson in his CListEditView class. This compiles but the edits still disappear as before. Incidently, I am only interested in editing the item in the first column, not any of the subitems.
I have attached a demo program for anyone interested in trying to get this to work.
Last edited by Mike Pliam; December 24th, 2011 at 06:24 PM.
If you don't want to edit sub-items and don't want to change some other default behavior, the only one notification message to be handled is LVN_ENDLABELEDIT:
void CEditableListCtrl::OnLvnEndlabeledit(NMHDR *pNMHDR, LRESULT *pResult)
NMLVDISPINFO *pDispInfo = reinterpret_cast<NMLVDISPINFO*>(pNMHDR);
// If the item label text has been modified...
if(NULL != pDispInfo->item.pszText)
//... let's put it in the list!
SetItemText(pDispInfo->item.iItem, 0, pDispInfo->item.pszText);
*pResult = 0;
Every compiler "allegedly not recognize CListVew as a class" just because the header file where CListVew class is defined (<afxcview.h>) is not included; anyhow, here you need CListCtrl and not CListView.
In my example, I didn't handle LVN_ENDLABELEDIT in the parent window class, but the reflected message (=LVN_ENDLABELEDIT) in a class derived from CListCtrl; this is a little bit more OOP approach; additionaly you may notice that the Wizard will help you more in this case.