Allowing Users to Edit Item Labels

To allow users to edit the item labels, the treeview control should be created with the TVS_EDITLABELS style. Either you can set this style in the resource editor, or in the call to Create() or in PreCreateWindow() if you are using the CTreeView class.

You should then handle the TVN_BEGINLABELEDIT and the TVN_ENDLABELEDIT notifications. You can use the class wizard to add the handler functions. Actually it is not necessary to handle the TVN_BEGINLABELEDIT notification but it is a good idea to do so and limit the length of the text that the user may enter into the edit control.


void COutline::OnBeginLabelEdit(NMHDR* pNMHDR, LRESULT* pResult)
{
TV_DISPINFO* pTVDispInfo = (TV_DISPINFO*)pNMHDR;

// Limit text to 127 characters
GetEditControl()->LimitText(127);

*pResult = 0;
}

It is necessary for you to handle the TVN_ENDLABELEDIT notification. If you don’t do so, then although the user will be able to edit the labels, but at the end of the edit the value of the label will be set to the previous value. The handler is also a good place to update any internal data structure you maintain to track the state of the tree view control.


void COutline::OnEndLabelEdit(NMHDR* pNMHDR, LRESULT* pResult)
{
// Set result to TRUE to accept the changes
*pResult = TRUE;
}

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read