// in class header
//..
CListCtrl m_MyListCtrl;
//..
// in class implementation
// void MyClass::Rename()
{
//..
int nItem;
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.
Please help.
Last edited by Mike Pliam; December 24th, 2011 at 02:47 AM.
Mike, don't you see a difference between "handling LVN_xxx" and "sending LVN_xxx". In LVN_, N stands for notification. Notifications are control "callback" messages, not command messages.
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:
Code:
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;
}
Aside notes:
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.
Basically, to make a listview control with editable item labels, we have only to:
Set the LVS_EDITLABELS style;
Handle LVN_ENDLABELEDIT; if the label text...
...is unchanged, pDispInfo->item.pszText is NULL;
...has been edited, pDispInfo->item.pszText is not NULL, then use its value to set the list item text.
All the rest is made by the control itself,
Probably, handling LVN_ENDLABELEDIT is let up to programmer, to allow performing additional formatting and validation.
{
//... let's save it for later use
m_csItemText = pDispInfo->item.pszText;
m_csOldName = m_csItemText;
}
but obviously I havn't figured out how to get the initial (m_csOldName) because this code invariably leads to m_csOldName as empty.
This code should give you the NEW label, if you look closely...
But before you set the new text to that item, you sure can get its current text.
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio: FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...
Thanks for the great help. The reason I need both old and new names is that this step is preliminary to changing the name of an item in another database.
Bookmarks