locked
CTreeCtrl, how to tell which node is selected. RRS feed

  • Question

  • If I have a CTreeCtrl in which there is one root node called "root_node" and two child nodes called "child_1" and "child_2," and my tree is setup to have checkboxes, how do I program it so that I can tell which checkbox has been selected?

    Under my fuction OnNMClickMcamx4InstallTree(NMHDR *pNMHDR, LRESULT *pResult) I have the following:

    HTREEITEM hCurrent = m_installTree.GetSelectedItem();  
     
    CString str;  
    str = m_installTree.GetItemText(hCurrent);  
    MessageBox((LPCTSTR) str);  
     

    Each time the program is run and I select a child node, the MessageBox always comes up with "root_node."  What do I need to do so that the proper node displays in my MessageBox?

    Thanks.
    Monday, November 3, 2008 7:28 PM

Answers

  • What you are describing is the default behavior. If you want to customize it, you need to handle the mouse click (NM_CLICK) and set the item state based on the result of hit test

    Before you call a behavior a bug, read the documentation to see if it is the designed behavior.

     

    MSMVP VC++
    • Marked as answer by jack 321 Monday, November 10, 2008 2:06 AM
    Friday, November 7, 2008 2:50 AM
  •   Thanks for the help.  I finally figured it and I'll post the solution here incase there are others who need to know.  To reiterate the previous post, after you select the checkbox item of the tree you have to call SelectItem(HTREEITEM) on it.  Here's what I have that works:

    void MyTreeDlg::OnItemClicked(NMHDR *pNMHDR, LRESULT *pResult)  
        {  
        HTREEITEM sel = m_myTree.GetSelectedItem();  
        CPoint point;     
        GetCursorPos (&point);  
        m_myTree.ScreenToClient (&point);  
        UINT flags = 0;  
        HTREEITEM hitItem = m_myTree.HitTest (point, &flags);  
        if (hitItem && sel != hitItem)  
            {  
            sel = hitItem;  
            m_myTree.SelectItem (sel);  
            CString str;  
            str = m_myTree.GetItemText (sel);  
            MessageBox((LPCTSTR) str);  // Just to verify.  
            }  
        } 

    And just for the record, I didn't call the normal behavior a bug, I asked if it was a bug.

    Thanks.
    • Marked as answer by jack 321 Monday, November 10, 2008 2:06 AM
    Friday, November 7, 2008 3:20 PM

All replies

  • checked state is different from selected state. It is not exclusive and the selected state is exclusive. You can get the check state by calling CTreeCtrl::GetCheck
    Don't know how you plan to handle multiple checked items.

    MSMVP VC++
    Monday, November 3, 2008 9:19 PM
  • I understand they are different.  Let me restate my question in a better way.

    When I call up the dialog which as the ctreectrl on it, the root node is automatically selected right off the bat.  That is fine.  Now, when I select the checkbox of a child node it doesn't change the currently selected item.  No matter what checkbox I click in, or how many checkboxes I click, the root node is always the one that is highlighted and selected.  However, if I click on the child node's text it be selected and highlighted.

    Is this a big with CTreeCtrl?  If not, how do you tell which node is selected?

    Thank you.

    Thursday, November 6, 2008 8:48 PM
  • What you are describing is the default behavior. If you want to customize it, you need to handle the mouse click (NM_CLICK) and set the item state based on the result of hit test

    Before you call a behavior a bug, read the documentation to see if it is the designed behavior.

     

    MSMVP VC++
    • Marked as answer by jack 321 Monday, November 10, 2008 2:06 AM
    Friday, November 7, 2008 2:50 AM
  •   Thanks for the help.  I finally figured it and I'll post the solution here incase there are others who need to know.  To reiterate the previous post, after you select the checkbox item of the tree you have to call SelectItem(HTREEITEM) on it.  Here's what I have that works:

    void MyTreeDlg::OnItemClicked(NMHDR *pNMHDR, LRESULT *pResult)  
        {  
        HTREEITEM sel = m_myTree.GetSelectedItem();  
        CPoint point;     
        GetCursorPos (&point);  
        m_myTree.ScreenToClient (&point);  
        UINT flags = 0;  
        HTREEITEM hitItem = m_myTree.HitTest (point, &flags);  
        if (hitItem && sel != hitItem)  
            {  
            sel = hitItem;  
            m_myTree.SelectItem (sel);  
            CString str;  
            str = m_myTree.GetItemText (sel);  
            MessageBox((LPCTSTR) str);  // Just to verify.  
            }  
        } 

    And just for the record, I didn't call the normal behavior a bug, I asked if it was a bug.

    Thanks.
    • Marked as answer by jack 321 Monday, November 10, 2008 2:06 AM
    Friday, November 7, 2008 3:20 PM