Question

How do we edit the item's labels and setting the edit control with the modified text?

I have been doing so but the item is not being override with the new text entered.Can anyone please help me out to achieve this by using win32.

This is what I had done.

if(((LPNMHDR)lParam)->code == TVN_BEGINLABELEDIT)
            {

                hEdit=TreeView_GetEditControl(hTree);
                SetFocus(hEdit);

            }
            if(((LPNMHDR)lParam)->code == TVN_ENDLABELEDIT)
            {

                char Text[256] = "";
                tvItem.hItem   = Selected;
                SendDlgItemMessage(hWnd,IDC_TREE,TVM_GETITEM,0,(LPARAM)&tvItem);
                GetWindowText(hEdit, Text, sizeof(Text)); 
                tvItem.pszText = Text;
                SendDlgItemMessage(hWnd,IDC_TREE,TVM_SETITEM,0,(LPARAM)&tvItem);


            }

What's the miracle you know it is showing the entered text in "tvItem.psText" ,but when I am setting the item it is not showing the item label modified and the label is same as old text.Can anyone please help to make that edited text appears on the item label.

Thanks In Advance, Siva V

Was it helpful?

Solution

Read the documentation, it tells you what you need to know:

lParam
Pointer to an NMTVDISPINFO structure. The item member of this structure is a TVITEM structure whose hItem, lParam, and pszText members contain valid information about the item that was edited. If label editing was canceled, the pszText member of the TVITEM structure is NULL; otherwise, pszText is the address of the edited text.

Return value

If the pszText member is non-NULL, return TRUE to set the item's label to the edited text. Return FALSE to reject the edited text and revert to the original label.

Remarks

If the pszText member is NULL, the return value is ignored.

If you specified the LPSTR_TEXTCALLBACK value for this item and the pszText member is non-NULL, your TVN_ENDLABELEDIT handler should copy the text from pszText to your local storage.

So, try something like this:

...
if (((LPNMHDR)lParam)->code == TVN_BEGINLABELEDIT)
{
    hEdit = TreeView_GetEditControl(hTree);
    SetFocus(hEdit);
    break;
}

if (((LPNMHDR)lParam)->code == TVN_ENDLABELEDIT)
{
    if ((LPNMTVDISPINFO)lParam)->item.pszText != NULL)
        return TRUE; // or FALSE to reject the next text...
    break;
}
...

OTHER TIPS

Below the snippet to stop editing on Escape/Enter keys.

// Global var
WNDPROC oldWndProc;
...
// In main WindowProc, WM_NOTIFY
NMHDR* pHdr = (LPNMHDR)lParam;
if (pHdr->code == TVN_BEGINLABELEDIT) {
    HWND hEdit = TreeView_GetEditControl(hTree);
    oldWndProc = (WNDPROC) SetWindowLongPtr(hEdit, GWL_WNDPROC, (LONG_PTR)&newWndProc);
}
...
LRESULT CALLBACK newWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    if (uMsg == WM_GETDLGCODE)
        return (DLGC_WANTALLKEYS | CallWindowProc(oldWndProc, hwnd, uMsg, wParam, lParam));

    return CallWindowProc(oldWndProc, hwnd, uMsg, wParam, lParam);
}

This code just shows you some clues.

LPNMHDR hdr;
NMLVDISPINFO *di;

hdr = (LPNMHDR)lParam;
di = (NMLVDISPINFO*)lParam;
...
...
switch(hdr->code)
{
...
case TVN_ENDLABELEDIT:
    if(di->tvItem.pszText != NULL)
    {
        //if you click 'Enter Key' after changing string, di->tvItem.pszText has  already changed string.
        //So, the edited string will change edit control label, by using 'return TRUE'
        return TRUE;
    }
    else
    {
        //if you cancel to edit by using 'Esc Key', di->tvItem.pszText value is NULL
        //So, the edited string will cancel, by using 'return FALSE'
        return FALSE;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top