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. Need help with sychronizing list control datas in different windows

Need help with sychronizing list control datas in different windows

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestionc++
5 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.
  • D Offline
    D Offline
    David Z
    wrote on last edited by
    #1

    Ok, I have been trying to get this to work for a while now. X| The response I got from my previous thread helped me in theory but didn't help my code.. :confused: so I'm posting a more detailed question if anyone can help me. I have a dialog based MFC app with a child listcontrol in report view. I then created another class for another window that will display another listcontrol but with the exact same column and data in it. My problem is that I can't copy the data over with what i've tried.. I have the user add item to the first list using SetItemData(). Like the following-> listitems.mask = LVIF_TEXT; listitems.iItem = 0; listitems.iSubItem = 0; listitems.pszText = (LPTSTR)(LPCTSTR)(strTitle); m_MyList.InsertItem(&listitems); listitems.mask = LVIF_TEXT; listitems.iSubItem = 1; listitems.pszText = (LPTSTR)(LPCTSTR)(m_strFirstName); m_MyList.SetItem(&listitems); ...... There are five columns and then when the user clicks a menu item it will open up another window showing the listcontrol that is supposed to show the same data. Here is the code for that-> void CMFCDlg::OnListSortbyday() { DWORD_PTR tempdata = m_MyList.GetItemData(0); GenList SortWindow; SortWindow.m_SortList.SetItemData(0,tempdata); SortWindow.DoModal(); } m_MyList is a public member CListCtrl of the main window, and m_SortList is a public member CListCtrl of the newly opened window. This is my most recent try at getting this to work... I get an assertion error when compiling though....:mad: My previous methods were declaring m_SortList as a CListCtrl pointer and point it to m_MyList, or setting the DDX_Control of the listview to CMFCDlg::m_MyList, but neither methods worked. I'm sure there is a way to get this to work.. Maybe making an instance of my main window.. but I don't know how to do that... So can someone guide me in the right direction? I don't want to just loop through and copy every single row, but I may have to.. Please help me out with some explicit instructions and sample code. Thanks!!:-D Sorry for the long post..

    D 1 Reply Last reply
    0
    • D David Z

      Ok, I have been trying to get this to work for a while now. X| The response I got from my previous thread helped me in theory but didn't help my code.. :confused: so I'm posting a more detailed question if anyone can help me. I have a dialog based MFC app with a child listcontrol in report view. I then created another class for another window that will display another listcontrol but with the exact same column and data in it. My problem is that I can't copy the data over with what i've tried.. I have the user add item to the first list using SetItemData(). Like the following-> listitems.mask = LVIF_TEXT; listitems.iItem = 0; listitems.iSubItem = 0; listitems.pszText = (LPTSTR)(LPCTSTR)(strTitle); m_MyList.InsertItem(&listitems); listitems.mask = LVIF_TEXT; listitems.iSubItem = 1; listitems.pszText = (LPTSTR)(LPCTSTR)(m_strFirstName); m_MyList.SetItem(&listitems); ...... There are five columns and then when the user clicks a menu item it will open up another window showing the listcontrol that is supposed to show the same data. Here is the code for that-> void CMFCDlg::OnListSortbyday() { DWORD_PTR tempdata = m_MyList.GetItemData(0); GenList SortWindow; SortWindow.m_SortList.SetItemData(0,tempdata); SortWindow.DoModal(); } m_MyList is a public member CListCtrl of the main window, and m_SortList is a public member CListCtrl of the newly opened window. This is my most recent try at getting this to work... I get an assertion error when compiling though....:mad: My previous methods were declaring m_SortList as a CListCtrl pointer and point it to m_MyList, or setting the DDX_Control of the listview to CMFCDlg::m_MyList, but neither methods worked. I'm sure there is a way to get this to work.. Maybe making an instance of my main window.. but I don't know how to do that... So can someone guide me in the right direction? I don't want to just loop through and copy every single row, but I may have to.. Please help me out with some explicit instructions and sample code. Thanks!!:-D Sorry for the long post..

      D Offline
      D Offline
      Derek Waters
      wrote on last edited by
      #2

      Hi, Your problem (which I assume is an assertion when running, not compiling) is that when you call SetItemData in your OnListSortByDay() function, the list control does not exist (if you look at the assertion, it will assert on (m_hWnd == NULL), ie. your control hasn't been created yet). All of the creation is done once you call DoModal. But of course, as it's modal, you can't call any subsequent functions in OnListSortByDay(). So, here's my suggestion. I'm sure other people will have others. In your GenList class, create a public function called SetCopyFromList, where you pass in a CListCtrl pointer:

      void GenList::SetCopyFromList(CListCtrl *apCopyDataFrom);

      In that function, store the pointer for later use in a member variable of GenList. Then, in the OnInitDialog() handler for GenList, you can use this pointer to obtain data for your list control, safe in the knowledge that m_SortList has been created. Another alternative would be to override GenList::DoModal() with another function such as GenList::DoModal(CListCtrl *apCopyDataFrom) and then call CDialog::DoModal() inside that function. Hope this helps. ------------------------ Derek Waters derek@lj-oz.com

      D 1 Reply Last reply
      0
      • D Derek Waters

        Hi, Your problem (which I assume is an assertion when running, not compiling) is that when you call SetItemData in your OnListSortByDay() function, the list control does not exist (if you look at the assertion, it will assert on (m_hWnd == NULL), ie. your control hasn't been created yet). All of the creation is done once you call DoModal. But of course, as it's modal, you can't call any subsequent functions in OnListSortByDay(). So, here's my suggestion. I'm sure other people will have others. In your GenList class, create a public function called SetCopyFromList, where you pass in a CListCtrl pointer:

        void GenList::SetCopyFromList(CListCtrl *apCopyDataFrom);

        In that function, store the pointer for later use in a member variable of GenList. Then, in the OnInitDialog() handler for GenList, you can use this pointer to obtain data for your list control, safe in the knowledge that m_SortList has been created. Another alternative would be to override GenList::DoModal() with another function such as GenList::DoModal(CListCtrl *apCopyDataFrom) and then call CDialog::DoModal() inside that function. Hope this helps. ------------------------ Derek Waters derek@lj-oz.com

        D Offline
        D Offline
        David Z
        wrote on last edited by
        #3

        Great Response Derek! You are right that the assertion error is at runtime, I got confused.. :) This definitely put me in the right direction. However, after I copied the list through item data, nothing is showing in the newly generated list... I just added this code to test if the data are being copied in an overridden OnInitDialog function-> LVITEM tempdata; CopiedList->GetItem(&tempdata); m_SortedList.InsertItem(&tempdata); No luck... Please help me solve this final obstacle. Thanks.

        P 1 Reply Last reply
        0
        • D David Z

          Great Response Derek! You are right that the assertion error is at runtime, I got confused.. :) This definitely put me in the right direction. However, after I copied the list through item data, nothing is showing in the newly generated list... I just added this code to test if the data are being copied in an overridden OnInitDialog function-> LVITEM tempdata; CopiedList->GetItem(&tempdata); m_SortedList.InsertItem(&tempdata); No luck... Please help me solve this final obstacle. Thanks.

          P Offline
          P Offline
          peterchen
          wrote on last edited by
          #4

          LVITEM tempdata; TCHAR textBuf[1024] = { 0 }; // unless your strings may be longer... // specify which item to copy: tempdata.iItem = ; tempdata.iSubItem = 0; // specify which data to copy: tempdata.mask = LVIF_IMAGE | LVIF_INDENT | LVIF_PARAM | LVIF_STATE | LVIF_TEXT; tempdata.stateMask = -1; tempdata.pszText = textBuf; tempdata.cchTextMax = 1023; CopiedList->GetItem(&tempdata); int insertIdx = m_SortedList.InsertItem(&tempdata); // idx where item was isnerted // also, you would need to copy sub item text manually... for(colIdx=1 to columns) { CString text = CopiedList->GetItemText( , colIdx); m_SortedList.SetItemText(insertIdx, colIdx, text); } -------------- completely different approach: probably it's to late to change now, but I'd used two virtual list controls for this Instead of feeding the list control, it asks you what to display when something is to be displayed. (However, you would need to sort the stuff yourself) Peter

          D 1 Reply Last reply
          0
          • P peterchen

            LVITEM tempdata; TCHAR textBuf[1024] = { 0 }; // unless your strings may be longer... // specify which item to copy: tempdata.iItem = ; tempdata.iSubItem = 0; // specify which data to copy: tempdata.mask = LVIF_IMAGE | LVIF_INDENT | LVIF_PARAM | LVIF_STATE | LVIF_TEXT; tempdata.stateMask = -1; tempdata.pszText = textBuf; tempdata.cchTextMax = 1023; CopiedList->GetItem(&tempdata); int insertIdx = m_SortedList.InsertItem(&tempdata); // idx where item was isnerted // also, you would need to copy sub item text manually... for(colIdx=1 to columns) { CString text = CopiedList->GetItemText( , colIdx); m_SortedList.SetItemText(insertIdx, colIdx, text); } -------------- completely different approach: probably it's to late to change now, but I'd used two virtual list controls for this Instead of feeding the list control, it asks you what to display when something is to be displayed. (However, you would need to sort the stuff yourself) Peter

            D Offline
            D Offline
            David Z
            wrote on last edited by
            #5

            Well, finally got it to work just copying every item through a loop. So I guess that'll have to do for now. Thanks for everyone's help. Now I just need to figure out how to render the listview based on the data given to it.. (Don't know if that's possible..) But that's for another thread... :)

            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