CTreeCtrl Help Please!
-
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
-
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
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
-
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
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
-
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
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.