Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All

I have a tree control. I want to do following thing on it.
When user right click on any tree item a popup menu is opened. User select 'Rename' menu from this and rename the tree item.

Can anybody tell me how to do this? I am using VS2005.
Posted
Comments
Jochen Arndt 21-Aug-13 5:00am    
What have you tried so far?
Do you know how to create and show a popup menu?
Do you know how to change the item text?
Vaibhav_J_Jaiswal 21-Aug-13 5:26am    
I popup the menu and add an event handler of it. Now I am trying to rename the selected tree item.
Jochen Arndt 21-Aug-13 5:44am    
Please use the Reply button right of the poster name. Than the recipient will get an email notification.

How did you handle the popup menu? A short code snippet would be helpful. You can use the green 'Improve question' link to edit your question.

A typical implemementation is handling WM_CONTEXTMENU messages in the control derived class. With tree controls you get the clicked item with GetSelectedItem(). Create the popup menu and show it using TrackPopupMenuEx with flag TPM_RETURNCMD. Then no menu handler is called but the selected menu item ID is returned. When the returned ID belongs to the rename item, call SetItemText passing the selected item.
Vaibhav_J_Jaiswal 21-Aug-13 6:15am    
menu.AppendMenu(MF_SEPARATOR, NULL);
menu.AppendMenu(MF_STRING, IDC_RENAME_ITEM, _T("Rename Item"));//Delaplex
try
{
if ( GetUsersItemPermissions(m_nhUser, data.nh).CanDelete()
&& GetUserSystemPermission(m_nhUser, CSystemPerm::SYSTEM) )
{
menu.AppendMenu(MF_SEPARATOR, NULL);
menu.AppendMenu(MF_STRING, IDC_DELETE_ITEM, GetResourceString(IDS_MENU_DELETE_ITEM));
}
}
catch (_com_error&)
{
}
Jochen Arndt 21-Aug-13 6:46am    
This does not help much to answer your question.
But I have added a solution that might help you.

A typical solution might look like this:
C++
BEGIN_MESSAGE_MAP(CMyTreeCtrl, CTreeCtrl)
    ON_WM_CONTEXTMENU()
END_MESSAGE_MAP()

void CMyTreeCtrl::OnContextMenu(CWnd* pWnd, CPoint point)
{
    HTREEITEM hCurSel = GetSelectedItem();
    if (point.x < 0 && point.y < 0) // if opened by keyboard (not mouse)
    {
        RECT rect;
        if (hCurSel)
        {
            GetItemRect(hCurSel, &rect, TRUE); // relative to control, text only
            ClientToScreen(&rect); // convert to screen coordinates
            point.SetPoint(rect.left, rect.bottom); // below selected item
        }
        else
        {
            GetWindowRect(&rect);
            point.SetPoint(rect.left, rect.top); // left/top of control
        }
    }
    else // Select the item that is at the clicked point
    {
        // When right clicking on the control we should get the focus first
        //  to show the active selection. 
        // Because many context commands use the selection (Copy, Cut, Clear).
        if (GetFocus() != this)
            SetFocus();
        CPoint pt(point); // we need window relative coordinates
        ScreenToClient(&pt);
        hCurSel = HitTest(pt);
        if (hCurSel != NULL)
            SelectItem(hCurSel);
        else // if not clicked on item (e.g. on scrollbar)
        { //  use default handling
            CTreeCtrl::OnContextMenu(pWnd, point);
            return;
        }
    }

    // Example loading menu from resources
    CMenu menu;
    VERIFY(menu.LoadMenu(IDR_MYTREECTRL_POPUP_MENU));
    CMenu *pSub = menu.GetSubMenu(0); // Get the pop-up menu.

    // Gray out or remove items according to various conditions
    if (NULL == hCurSel)
        pSub->EnableMenuItem(IDC_RENAME_ITEM, MF_GRAYED);

    // Show the popup menu.
    // Example uses TPM_RETURNCMD to return the ID of the selected menu item
    //  rather than calling the handler.
    int nCommand = pSub->TrackPopupMenuEx(
        TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_VERPOSANIMATION | TPM_RETURNCMD | TPM_NONOTIFY,
        point.x, point.y, 
        AfxGetMainWnd(), NULL);

    switch (nCommand)
    {
    case IDC_RENAME_ITEM : // rename menu item
        SetItemText(hCurSel, _T("New item text"));
        break;
    default : // all other menu items
        if (nCommand)
            SendMessage(WM_COMMAND, nCommand);
    }
}
 
Share this answer
 
On the form with treeview, create object for System.Windows.Forms.ContextMenuStrip (Its on your toolbox)

Set treeview's ContextMenuStrip property to this object.

Add menu item Rename on the Context menu strip.

Handle newly added menu's OnClick event....
 
Share this answer
 
Comments
Vaibhav_J_Jaiswal 21-Aug-13 5:29am    
I do this already. Now I want to rename the selected tree item. What I have to do in this menu event for rename the selected tree item.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900