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. Problem with list control

Problem with list control

Scheduled Pinned Locked Moved C / C++ / MFC
databasehelptutorialquestion
8 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.
  • Z Offline
    Z Offline
    zhaopi
    wrote on last edited by
    #1

    I am declaring a list and add in contents into it~ But i am always unpredictably facing the display problem~ For example, i am reading information from a database entry by entry, but what is displayed in the list control is totally out of order, and correspondance is wrong. what is worse, some of the colums are blank though i can see the entries are correctly inserted into the list(i have traced this using break point). The code i used is as below: (m_bList is the list control declared) int index; for(index = 0; index < total; index++) { LV_ITEM lvi; lvi.mask = LVIF_TEXT; lvi.iItem = index; lvi.iSubItem = 0; lvi.pszText = (LPSTR)(LPCSTR)sbatchid; lvi.lParam = 1; m_bList.InsertItem(&lvi); lvi.iSubItem = 1; lvi.pszText = (LPSTR)(LPCSTR)page;m_bList.SetItem(&lvi); CString modi = time.Format("%Y/%m/%d/%H:%M"); lvi.iSubItem = 2; lvi.pszText = (LPSTR)(LPCSTR)modi; m_bList.SetItem(&lvi); }

    R D 3 Replies Last reply
    0
    • Z zhaopi

      I am declaring a list and add in contents into it~ But i am always unpredictably facing the display problem~ For example, i am reading information from a database entry by entry, but what is displayed in the list control is totally out of order, and correspondance is wrong. what is worse, some of the colums are blank though i can see the entries are correctly inserted into the list(i have traced this using break point). The code i used is as below: (m_bList is the list control declared) int index; for(index = 0; index < total; index++) { LV_ITEM lvi; lvi.mask = LVIF_TEXT; lvi.iItem = index; lvi.iSubItem = 0; lvi.pszText = (LPSTR)(LPCSTR)sbatchid; lvi.lParam = 1; m_bList.InsertItem(&lvi); lvi.iSubItem = 1; lvi.pszText = (LPSTR)(LPCSTR)page;m_bList.SetItem(&lvi); CString modi = time.Format("%Y/%m/%d/%H:%M"); lvi.iSubItem = 2; lvi.pszText = (LPSTR)(LPCSTR)modi; m_bList.SetItem(&lvi); }

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

      zhaopi wrote: what is displayed in the list control is totally out of order First thought (without reading your code) have you disabled the "Sorted" option of the list control ? ~RaGE();

      1 Reply Last reply
      0
      • Z zhaopi

        I am declaring a list and add in contents into it~ But i am always unpredictably facing the display problem~ For example, i am reading information from a database entry by entry, but what is displayed in the list control is totally out of order, and correspondance is wrong. what is worse, some of the colums are blank though i can see the entries are correctly inserted into the list(i have traced this using break point). The code i used is as below: (m_bList is the list control declared) int index; for(index = 0; index < total; index++) { LV_ITEM lvi; lvi.mask = LVIF_TEXT; lvi.iItem = index; lvi.iSubItem = 0; lvi.pszText = (LPSTR)(LPCSTR)sbatchid; lvi.lParam = 1; m_bList.InsertItem(&lvi); lvi.iSubItem = 1; lvi.pszText = (LPSTR)(LPCSTR)page;m_bList.SetItem(&lvi); CString modi = time.Format("%Y/%m/%d/%H:%M"); lvi.iSubItem = 2; lvi.pszText = (LPSTR)(LPCSTR)modi; m_bList.SetItem(&lvi); }

        R Offline
        R Offline
        Rage
        wrote on last edited by
        #3

        Second thought, after reading the code : It is a safe thing to zero the LVITEM struct (you wrote LV_ITEM, but it is in fact LVITEM) before using it.

        LVITEM lvi;
        ZeroMemory(&lvi,sizeof(lvi));

        It is also not a bad idea to check for the result of the InsertItem and SetItem functions before going on. Last thing to do is maybe to prevent updating the control before it is entirely full, or use virtual list if you are dealing with database entries (search on Code Project for virtual lists). But the code seems OK to my point of view. You should really check that the Sort ComboBox in the Properties of the control is set on "None" (in the VC++ Dialog Editor). Or programatically unset the LVS_SORTASCENDING and LVS_SORTDESCENDING window styles of the control. ~RaGE();

        Z 1 Reply Last reply
        0
        • R Rage

          Second thought, after reading the code : It is a safe thing to zero the LVITEM struct (you wrote LV_ITEM, but it is in fact LVITEM) before using it.

          LVITEM lvi;
          ZeroMemory(&lvi,sizeof(lvi));

          It is also not a bad idea to check for the result of the InsertItem and SetItem functions before going on. Last thing to do is maybe to prevent updating the control before it is entirely full, or use virtual list if you are dealing with database entries (search on Code Project for virtual lists). But the code seems OK to my point of view. You should really check that the Sort ComboBox in the Properties of the control is set on "None" (in the VC++ Dialog Editor). Or programatically unset the LVS_SORTASCENDING and LVS_SORTDESCENDING window styles of the control. ~RaGE();

          Z Offline
          Z Offline
          zhaopi
          wrote on last edited by
          #4

          Thanks for your reply first~ I have checked the sort combobox, unfortunately it is set to none. For the result of insertitem, it is always returning 0 which seems to be fine~ and the setitem function is returning 1 for successful. And how should i use zeromemory function, since it seems to be deleting the LVITEM and all the items are failed to be displayed in the list~

          R 1 Reply Last reply
          0
          • Z zhaopi

            Thanks for your reply first~ I have checked the sort combobox, unfortunately it is set to none. For the result of insertitem, it is always returning 0 which seems to be fine~ and the setitem function is returning 1 for successful. And how should i use zeromemory function, since it seems to be deleting the LVITEM and all the items are failed to be displayed in the list~

            R Offline
            R Offline
            Rage
            wrote on last edited by
            #5

            zhaopi wrote: And how should i use zeromemory function, s For initialisation of your lvi variable. Use it just once, after the variable definition:

            LVITEM lvi;
            ZeroMemory(&lvi,sizeof(lvi));

            Now you can use lvi safely. ~RaGE();

            1 Reply Last reply
            0
            • Z zhaopi

              I am declaring a list and add in contents into it~ But i am always unpredictably facing the display problem~ For example, i am reading information from a database entry by entry, but what is displayed in the list control is totally out of order, and correspondance is wrong. what is worse, some of the colums are blank though i can see the entries are correctly inserted into the list(i have traced this using break point). The code i used is as below: (m_bList is the list control declared) int index; for(index = 0; index < total; index++) { LV_ITEM lvi; lvi.mask = LVIF_TEXT; lvi.iItem = index; lvi.iSubItem = 0; lvi.pszText = (LPSTR)(LPCSTR)sbatchid; lvi.lParam = 1; m_bList.InsertItem(&lvi); lvi.iSubItem = 1; lvi.pszText = (LPSTR)(LPCSTR)page;m_bList.SetItem(&lvi); CString modi = time.Format("%Y/%m/%d/%H:%M"); lvi.iSubItem = 2; lvi.pszText = (LPSTR)(LPCSTR)modi; m_bList.SetItem(&lvi); }

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

              How about:

              for (int index = 0; index < total; index++)
              {
              int item = m_bList.InsertItem(index, 0, sbatchid);
              m_bList.SetItemText(item, 1, page);
              m_bList.SetItemText(item, 2, time.Format("%Y/%m/%d/%H:%M"));
              }


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

              Z 1 Reply Last reply
              0
              • D David Crow

                How about:

                for (int index = 0; index < total; index++)
                {
                int item = m_bList.InsertItem(index, 0, sbatchid);
                m_bList.SetItemText(item, 1, page);
                m_bList.SetItemText(item, 2, time.Format("%Y/%m/%d/%H:%M"));
                }


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

                Z Offline
                Z Offline
                zhaopi
                wrote on last edited by
                #7

                it works, but i really cannot see what is the difference between your method and mine~ Can explain a little bit~? Thanks

                R 1 Reply Last reply
                0
                • Z zhaopi

                  it works, but i really cannot see what is the difference between your method and mine~ Can explain a little bit~? Thanks

                  R Offline
                  R Offline
                  Rage
                  wrote on last edited by
                  #8

                  I think the difference is that David uses the index returned by InsertItem to access the associated subitems, whereas you use the loop index, which is then not necessarily equal to the inserted Item index. :doh: Meaning if loop index is 3, and inserted Item gets index 2, you will then modify subitem of Item with index 3 instead of 2. Is this clear :~ ? ~RaGE();

                  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