Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CTreeCtrl Help Please!

CTreeCtrl Help Please!

Scheduled Pinned Locked Moved C / C++ / MFC
helpdata-structuresquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vu Nguyen
    wrote on last edited by
    #1

    Hi all! I made a tree in my dialog. I successfully inserted a few nodes to the tree. The problems arise when I try to access the node that is being selected. I always get a NULL pointer when I try to get the handle to the selected node. Can any one tell me what I miss? When I insert the nodes, I ignore the *hItem* entry. Is it the cause, if so , how do you fix this? Here is what I did. I used 2 ways and they both give me a null pointer return: void CMyDlg::OnClickTreeview(NMHDR* pNMHDR, LRESULT* pResult) { NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR; HTREEITEM hSelectedNode = pNMTreeView->itemNew.hItem; //get the handle to the selected node if (!hSelectedNode) AfxMessageBox("fail to get the handle"); else AfxMessageBox("Get the handle OK"); } //***************************** another way is: CTreeCtrl * pTree = (CTreeCtrl*) GetDlgItem(IDC_TREE); HTREEITEM hSelectedNode = pTree->GetSelectedItem(); //******************************* Thank You Vu vucsuf

    R 1 Reply Last reply
    0
    • V Vu Nguyen

      Hi all! I made a tree in my dialog. I successfully inserted a few nodes to the tree. The problems arise when I try to access the node that is being selected. I always get a NULL pointer when I try to get the handle to the selected node. Can any one tell me what I miss? When I insert the nodes, I ignore the *hItem* entry. Is it the cause, if so , how do you fix this? Here is what I did. I used 2 ways and they both give me a null pointer return: void CMyDlg::OnClickTreeview(NMHDR* pNMHDR, LRESULT* pResult) { NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR; HTREEITEM hSelectedNode = pNMTreeView->itemNew.hItem; //get the handle to the selected node if (!hSelectedNode) AfxMessageBox("fail to get the handle"); else AfxMessageBox("Get the handle OK"); } //***************************** another way is: CTreeCtrl * pTree = (CTreeCtrl*) GetDlgItem(IDC_TREE); HTREEITEM hSelectedNode = pTree->GetSelectedItem(); //******************************* Thank You Vu vucsuf

      R Offline
      R Offline
      Rassman
      wrote on last edited by
      #2

      Here's some bits for you. Changed selection void CExIncView::OnSelchangedTree1(NMHDR* pNMHDR, LRESULT* pResult) { TVITEM ThisID; NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR; //get itemID. ThisID = pNMTreeView->itemNew; //Some extra bits for you GetTreePath(); SetFileList(); *pResult = 0; } Traverse tree creating a tree path string void CExIncView::GetTreePath() { CString lString; CString sTemp,sTemp2; HTREEITEM lItem; DWORD Code; lItem = m_pDirTree.GetSelectedItem(); sTemp = m_pDirTree.GetItemText(lItem); Code = m_pDirTree.GetItemData(lItem); lString = sTemp; while( (lItem=m_pDirTree.GetParentItem(lItem)) != NULL ) { sTemp = m_pDirTree.GetItemText(lItem); Code = m_pDirTree.GetItemData(lItem); //can make use of the ItemData to trigger different // code branches and pass some bit data sTemp2 = lString; lString.Format("%s\\%s",sTemp, sTemp2); } m_Filename = lString; // UpdateData(FALSE); } Find a string item in my tree. Actually I nearly always keep tree data in a CList or one of its type, seaches in a control with a lot of data can be slow for everyday use. Plus, you can store TV_ITEM objects in your List as items are added so that you can search and then select you tree item very quickly. This first function just shows a way to traverse. It calls a 2nd which finds the item by name Only put this as a quick way to change my Clist search to a tree search const int MAX_ITTERATIONS = 100; void CExIncView::FindInTree(CString &sText) { bool Done = FALSE; int j,Pos; char *lTreeNames[MAX_ITTERATIONS]; CString lString; //split the directory name into individual strings for(j=0;j

      V 1 Reply Last reply
      0
      • R Rassman

        Here's some bits for you. Changed selection void CExIncView::OnSelchangedTree1(NMHDR* pNMHDR, LRESULT* pResult) { TVITEM ThisID; NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR; //get itemID. ThisID = pNMTreeView->itemNew; //Some extra bits for you GetTreePath(); SetFileList(); *pResult = 0; } Traverse tree creating a tree path string void CExIncView::GetTreePath() { CString lString; CString sTemp,sTemp2; HTREEITEM lItem; DWORD Code; lItem = m_pDirTree.GetSelectedItem(); sTemp = m_pDirTree.GetItemText(lItem); Code = m_pDirTree.GetItemData(lItem); lString = sTemp; while( (lItem=m_pDirTree.GetParentItem(lItem)) != NULL ) { sTemp = m_pDirTree.GetItemText(lItem); Code = m_pDirTree.GetItemData(lItem); //can make use of the ItemData to trigger different // code branches and pass some bit data sTemp2 = lString; lString.Format("%s\\%s",sTemp, sTemp2); } m_Filename = lString; // UpdateData(FALSE); } Find a string item in my tree. Actually I nearly always keep tree data in a CList or one of its type, seaches in a control with a lot of data can be slow for everyday use. Plus, you can store TV_ITEM objects in your List as items are added so that you can search and then select you tree item very quickly. This first function just shows a way to traverse. It calls a 2nd which finds the item by name Only put this as a quick way to change my Clist search to a tree search const int MAX_ITTERATIONS = 100; void CExIncView::FindInTree(CString &sText) { bool Done = FALSE; int j,Pos; char *lTreeNames[MAX_ITTERATIONS]; CString lString; //split the directory name into individual strings for(j=0;j

        V Offline
        V Offline
        Vu Nguyen
        wrote on last edited by
        #3

        Hi There! Thank you for your help. The problem I have is different. I have a tree structure in the background. I then use SetItemData() to hook each node to a node in my backGround tree. When a node of CTreeCtrl is clicked, I display the coresponding node data in my backGround Tree. In the OnClick() event, I need to get the handle to the node that is selected. The problem I am having is that getSelectedItem() returns NULL at the beginning. After a few clicks, it seems to give me correct handle. I don't know if the search process is too slow or because the SetItemData screw me up. Do you have any clue? Thanks Vu vucsuf

        R 1 Reply Last reply
        0
        • V Vu Nguyen

          Hi There! Thank you for your help. The problem I have is different. I have a tree structure in the background. I then use SetItemData() to hook each node to a node in my backGround tree. When a node of CTreeCtrl is clicked, I display the coresponding node data in my backGround Tree. In the OnClick() event, I need to get the handle to the node that is selected. The problem I am having is that getSelectedItem() returns NULL at the beginning. After a few clicks, it seems to give me correct handle. I don't know if the search process is too slow or because the SetItemData screw me up. Do you have any clue? Thanks Vu vucsuf

          R Offline
          R Offline
          Rassman
          wrote on last edited by
          #4

          I suspect that you are trying to GetSelectedItem() actually between the change, there's a point at which none is selected. You wrote the function as getSelectedItem(), are you doing this in java? You, you trator you (only joking man). Anyway, check there is a selection first then with maybe a hit test. We do it for the joy of seeing the users struggle.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups