Editing listview subitems using LVM_GETEDITCONTROL | CodeGuru

Editing listview subitems using LVM_GETEDITCONTROL

  This artical suggests yet another way to edit subitems in CListView or CListCtrl. It uses LVM_GETEDITCONTROL message to return the handle of the actual edit control used by MFC and then subclasses the handle in order to re-postition the control over the subitem you wish to edit. The sample download program was written using […]

Written By
CodeGuru Staff
CodeGuru Staff
Nov 21, 1998
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

 

This artical suggests yet another way to edit subitems in CListView or CListCtrl. It uses LVM_GETEDITCONTROL message to return the handle of the actual edit control used by MFC and then subclasses the handle in order to re-postition the control over the subitem you wish to edit. The sample download program was written using MSVC 5.0 and includes a class called CListEditView, derived from CListView. The code can be modified to use CListCtrl as well. CListEditView::DrawItem(LPDRAWITEMSTRUCT lpDIS) is the slightly modified version taken from the ROWLIST example provided by MSVC 5.0. To use CListEditView in your project, just include the source and header files, and derive your list view class from CListEditView. Please feel free to modify the code as you see fit. Basically, it works as follows: When the handler for LVN_BEGINLABELEDIT is called, the handler locates the subitem from the current mouse position. It then retrieves the handle to the edit control, subclasses the control to a CEdit object and repositions the control over the location of the subitem. The subclassed CEdit object has the ON_WM_WINDOWPOSCHANGING handler overridden to use the coordinates of location of the subitem because it will try to reposition itself over the LABEL of the listview, when it becomes visible. When finished editing, the LVN_ENDLABELEDIT handler is called and if the operation was not aborted (by pressing ESC for example), the handler sets the subitem text to the contents in the edit control. I’ve included the source code for these two handlers below.

void CListEditView::OnBeginLabelEdit(NMHDR* pNMHDR,LRESULT* pResult)
{
    LV_DISPINFO* pDispInfo=(LV_DISPINFO*)pNMHDR;

*pResult=1;

CPoint posMouse;
GetCursorPos(&posMouse);
ScreenToClient(&posMouse);

LV_COLUMN lvc;
lvc.mask=LVCF_WIDTH;

CRect rcItem;
GetListCtrl().GetItemRect(pDispInfo->item.iItem,rcItem,LVIR_LABEL);

if(rcItem.PtInRect(posMouse))
m_nEdit=0;

int nCol=1;
while(m_nEdit==-1 && GetListCtrl().GetColumn(nCol,&lvc))
{
rcItem.left=rcItem.right;
rcItem.right+=lvc.cx;

if(rcItem.PtInRect(posMouse))
m_nEdit=nCol;

nCol++;
}

if(m_nEdit==-1)
return;

HWND hWnd=(HWND)SendMessage(LVM_GETEDITCONTROL);
ASSERT(hWnd!=NULL);
VERIFY(m_LVEdit.SubclassWindow(hWnd));

m_LVEdit.m_x=rcItem.left;
m_LVEdit.m_y=rcItem.top-1;

m_LVEdit.SetWindowText(GetListCtrl().GetItemText(pDispInfo->item.iItem,m_nEdit));

*pResult=0;
}

void CListEditView::OnEndLabelEdit(NMHDR* pNMHDR, LRESULT* pResult)
{
LV_DISPINFO* pDispInfo=(LV_DISPINFO*)pNMHDR;

CString sEdit=pDispInfo->item.pszText;

if(!sEdit.IsEmpty())
{
GetListCtrl().SetItemText(pDispInfo->item.iItem,m_nEdit,sEdit);
}

m_nEdit=-1;

VERIFY(m_LVEdit.UnsubclassWindow()!=NULL);

GetListCtrl().SetItemState(pDispInfo->item.iItem,0,LVNI_FOCUSED|LVNI_SELECTED);

*pResult=0;
}

Download demo project – 42 KB

Download source – 4 KB

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.