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. Help with SetItemData/GetItemData of CTreeCtrl

Help with SetItemData/GetItemData of CTreeCtrl

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresdebugginghelpquestion
3 Posts 3 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.
  • J Offline
    J Offline
    johnnyXP
    wrote on last edited by
    #1

    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?

    D 1 Reply Last reply
    0
    • J johnnyXP

      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?

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      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 in OnNMDblclkTreeTracks(). Memory was allocated in OnBnClickedOk().


      "One must learn from the bite of the fire to leave it alone." - Native American Proverb

      A 1 Reply Last reply
      0
      • D David Crow

        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 in OnNMDblclkTreeTracks(). Memory was allocated in OnBnClickedOk().


        "One must learn from the bite of the fire to leave it alone." - Native American Proverb

        A Offline
        A Offline
        Anonymous
        wrote on last edited by
        #3

        Daim you are right! The data is not the same for each node. I do that just to simplify the question and the code. Thanks a lot for your time.

        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