old problem with CListCtrl::EditLabel
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: old problem with CListCtrl::EditLabel

  1. #1
    Join Date
    May 2002
    Posts
    1,723

    old problem with CListCtrl::EditLabel

    I have looked over many posts and articles on this problem without being able to solve it. I have a CListCtrl created with the following styles:
    Code:
    	DWORD dwListStyle = LVS_REPORT | LVS_SORTASCENDING | LVS_SHOWSELALWAYS | WS_CHILD | WS_VISIBLE | LVS_EDITLABELS ;
    When I attempt to edit an entry, it appears to edit just fine, but when I hit return following the edit, the former label reappears.

    I have attempted to follow the advice posted by others indicating the need for 'handling' LVN_BEGINLABELEDIT and LVN_ENDLABELEDIT.

    http://www.codeguru.com/forum/showth...trl+edit+label

    http://www.codeguru.com/Cpp/controls...icle.php/c897/

    My attempts to do this look like this:
    Code:
    // 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.
    mpliam

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,971

    Re: old problem with CListCtrl::EditLabel

    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.

    Did you read this Editing listview subitems using LVM_GETEDITCONTROL that golanshahar recommended there in the link you found?
    Best regards,
    Igor

  3. #3
    Join Date
    May 2002
    Posts
    1,723

    Re: old problem with CListCtrl::EditLabel

    Thanks for pointing out my misinterpretation of 'handle'.

    Yes, I have read and reread the references noted and have experimented extensively, especially:

    Editing listview subitems using LVM_GETEDITCONTROL
    By Reg Anderson November 21, 1998
    http://www.codeguru.com/Cpp/controls...cle.php/c1077/

    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
    Code:
    	afx_msg void OnEndLabelEdit(NMHDR* pNMHDR,LRESULT* pResult);
    	afx_msg void OnBeginLabelEdit(NMHDR* pNMHDR,LRESULT* pResult);
    }
    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.
    Attached Files Attached Files
    Last edited by Mike Pliam; December 24th, 2011 at 06:24 PM.
    mpliam

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,286

    Re: old problem with CListCtrl::EditLabel

    Mike, why don't you use the sample I made for you about a year or two ago?

  5. #5
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,021

    Re: old problem with CListCtrl::EditLabel

    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:
    1. 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.
    2. 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.
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

  6. #6
    Join Date
    May 2002
    Posts
    1,723

    Re: old problem with CListCtrl::EditLabel

    Thanks, Ovi. Remarkably simple and effective solution.

    I now find that I need to identify the original (pre-edited) item label and have been having trouble doing that. I've tried :
    Code:
    void CEditableListCtrl::OnLvnBeginlabeledit(NMHDR *pNMHDR, LRESULT *pResult)
    {
    	NMLVDISPINFO *pDispInfo = reinterpret_cast<NMLVDISPINFO*>(pNMHDR);
    	if(NULL != pDispInfo->item.pszText)
    	{
    		//... let's save it for later use
    		m_csItemText = pDispInfo->item.pszText;
    		m_csOldName = m_csItemText;
    
    	}
    	*pResult = 0;
    }
    but obviously I havn't figured out how to get the initial (m_csOldName) because this code invariably leads to m_csOldName as empty.

    Appreciate any suggestions.
    Last edited by Mike Pliam; December 26th, 2011 at 06:25 PM.
    mpliam

  7. #7
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,021

    Re: old problem with CListCtrl::EditLabel

    If you really, really need to "keeep in mind" the old label text, you can do something like this:
    Code:
    void CEditableListCtrl::OnLvnBeginlabeledit(NMHDR *pNMHDR, LRESULT *pResult)
    {
       NMLVDISPINFO *pDispInfo = reinterpret_cast<NMLVDISPINFO*>(pNMHDR);
       m_strOldLabelText = GetItemText(pDispInfo->item.iItem, 0);
    
       *pResult = 0;
    }
    Usually that's not necessary.

    Basically, to make a listview control with editable item labels, we have only to:
    1. Set the LVS_EDITLABELS style;
    2. 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.
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

  8. #8
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,526

    Re: old problem with CListCtrl::EditLabel

    Quote Originally Posted by Mike Pliam View Post
    Code:
    {
    	//... 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...

  9. #9
    Join Date
    May 2002
    Posts
    1,723

    Re: old problem with CListCtrl::EditLabel

    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.
    mpliam

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center