Help with SetItemData/GetItemData of CTreeCtrl
-
I have a dialog based class named CJKDlg, with a tree control with name treeTracks. In the JKDlg.h i have defined the following structure:
struct _itemData { CString strData; };
In the OK click event i have the following handler:void CJKDlg::OnBnClickedOk() { CStdioFile stdFile; CString str; if(! stdFile.Open("test.txt", CFile::modeRead)) { AfxMessageBox("Cannot find initialization file", MB_OK, MB_ICONSTOP); return; } TVINSERTSTRUCT tvInsert; tvInsert.hParent = NULL; tvInsert.hInsertAfter = NULL; tvInsert.item.mask = TVIF_TEXT; HTREEITEM hAlbum, hItem; char ch; CString albumTitle, trackTitle, trackPath; _itemData* pItemData = new _itemData(); while(stdFile.ReadString(str)) { ch = str.GetAt(0); if(ch=='$') { albumTitle = str.Right(str.GetLength()-1); //TRACE1("%s\n", albumTitle); tvInsert.item.pszText = albumTitle.GetBuffer(albumTitle.GetLength()); hAlbum = m_treeTracks.InsertItem(&tvInsert); pItemData->strData = "OK"; m_treeTracks.SetItemData(hAlbum, DWORD(pItemData)); } } delete pItemData; }
In last a double-click event handler for the tree control:void CJKDlg::OnNMDblclkTreeTracks(NMHDR *pNMHDR, LRESULT *pResult) { HTREEITEM hItem = m_treeTracks.GetSelectedItem(); ASSERT(hItem); _itemData* pItemData = new _itemData(); pItemData = (_itemData *)m_treeTracks.GetItemData(hItem); if(pItemData) { TRACE1("%s\n", pItemData->strData); } delete pItemData; *pResult = 0; }
The TRACE macro dose not display the "OK" string. Instead in the output window a white space displayed. Can anyone help me with this please? -
I have a dialog based class named CJKDlg, with a tree control with name treeTracks. In the JKDlg.h i have defined the following structure:
struct _itemData { CString strData; };
In the OK click event i have the following handler:void CJKDlg::OnBnClickedOk() { CStdioFile stdFile; CString str; if(! stdFile.Open("test.txt", CFile::modeRead)) { AfxMessageBox("Cannot find initialization file", MB_OK, MB_ICONSTOP); return; } TVINSERTSTRUCT tvInsert; tvInsert.hParent = NULL; tvInsert.hInsertAfter = NULL; tvInsert.item.mask = TVIF_TEXT; HTREEITEM hAlbum, hItem; char ch; CString albumTitle, trackTitle, trackPath; _itemData* pItemData = new _itemData(); while(stdFile.ReadString(str)) { ch = str.GetAt(0); if(ch=='$') { albumTitle = str.Right(str.GetLength()-1); //TRACE1("%s\n", albumTitle); tvInsert.item.pszText = albumTitle.GetBuffer(albumTitle.GetLength()); hAlbum = m_treeTracks.InsertItem(&tvInsert); pItemData->strData = "OK"; m_treeTracks.SetItemData(hAlbum, DWORD(pItemData)); } } delete pItemData; }
In last a double-click event handler for the tree control:void CJKDlg::OnNMDblclkTreeTracks(NMHDR *pNMHDR, LRESULT *pResult) { HTREEITEM hItem = m_treeTracks.GetSelectedItem(); ASSERT(hItem); _itemData* pItemData = new _itemData(); pItemData = (_itemData *)m_treeTracks.GetItemData(hItem); if(pItemData) { TRACE1("%s\n", pItemData->strData); } delete pItemData; *pResult = 0; }
The TRACE macro dose not display the "OK" string. Instead in the output window a white space displayed. Can anyone help me with this please?johnnyXP wrote: m_treeTracks.SetItemData(hAlbum, DWORD(pItemData)); You are inserting the same
pItemData
for each node in the tree. Is that intentional? johnnyXP wrote: delete pItemData; This is why nothing shows up. johnnyXP wrote: _itemData* pItemData = new _itemData(); This statement is unnecessary inOnNMDblclkTreeTracks()
. Memory was allocated inOnBnClickedOk()
.
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
-
johnnyXP wrote: m_treeTracks.SetItemData(hAlbum, DWORD(pItemData)); You are inserting the same
pItemData
for each node in the tree. Is that intentional? johnnyXP wrote: delete pItemData; This is why nothing shows up. johnnyXP wrote: _itemData* pItemData = new _itemData(); This statement is unnecessary inOnNMDblclkTreeTracks()
. Memory was allocated inOnBnClickedOk()
.
"One must learn from the bite of the fire to leave it alone." - Native American Proverb