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. can't delete all items from CListBox

can't delete all items from CListBox

Scheduled Pinned Locked Moved C / C++ / MFC
htmlcomtutorialquestion
6 Posts 5 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.
  • K Offline
    K Offline
    kfaday
    wrote on last edited by
    #1

    i've got this code from MSDN http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/\_mfc\_clistbox.3a3a.deleteitem.asp

    for (int i=0;i < pmyListBox->GetCount();i++)
    {
    pmyListBox->DeleteString( i );
    }

    but it doesn't work, it deletes almost every item, but not all of them. It doesn't delete all items on the list. Any ideas how to do that?

    P R S 3 Replies Last reply
    0
    • K kfaday

      i've got this code from MSDN http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/\_mfc\_clistbox.3a3a.deleteitem.asp

      for (int i=0;i < pmyListBox->GetCount();i++)
      {
      pmyListBox->DeleteString( i );
      }

      but it doesn't work, it deletes almost every item, but not all of them. It doesn't delete all items on the list. Any ideas how to do that?

      P Offline
      P Offline
      palbano
      wrote on last edited by
      #2

      pmyListBox->ResetContent();

      "No matter where you go, there your are." - Buckaroo Banzai

      -pete

      K 1 Reply Last reply
      0
      • P palbano

        pmyListBox->ResetContent();

        "No matter where you go, there your are." - Buckaroo Banzai

        -pete

        K Offline
        K Offline
        kfaday
        wrote on last edited by
        #3

        thanks that did it

        L 1 Reply Last reply
        0
        • K kfaday

          thanks that did it

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Sometimes, you need to see each item before deleting it (like to delete data associated with the item data pointer). The reason the original code didn't work was that deleting item 0 then moves all the other items up one. Then you delete item 1 and the same thing happens to items 2-n. You probably ended up with opnly about half the items deleted. The solution is to either go through the list backwards, or to make the loop like this: while(lbx.GetCount()) { Data* p = (Data*)lbx.GetItemDataPtr(0); // do something with p; lbx.DeleteString(0); }

          1 Reply Last reply
          0
          • K kfaday

            i've got this code from MSDN http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/\_mfc\_clistbox.3a3a.deleteitem.asp

            for (int i=0;i < pmyListBox->GetCount();i++)
            {
            pmyListBox->DeleteString( i );
            }

            but it doesn't work, it deletes almost every item, but not all of them. It doesn't delete all items on the list. Any ideas how to do that?

            R Offline
            R Offline
            Ravi Bhavnani
            wrote on last edited by
            #5

            This is buggy - you want to do DeleteString(0) not DeleteString(i) since GetCount() will return you smaller values each time you call DeleteString(). Or as has already been pointed out, you can just use ResetContent(). /ravi My new year's resolution: 2048 x 1536 Home | Articles | Freeware | Music ravib@ravib.com

            1 Reply Last reply
            0
            • K kfaday

              i've got this code from MSDN http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/\_mfc\_clistbox.3a3a.deleteitem.asp

              for (int i=0;i < pmyListBox->GetCount();i++)
              {
              pmyListBox->DeleteString( i );
              }

              but it doesn't work, it deletes almost every item, but not all of them. It doesn't delete all items on the list. Any ideas how to do that?

              S Offline
              S Offline
              Shi Zhu
              wrote on last edited by
              #6

              for(int i=0;iGetCount()-1;i++) msn:Flashcutreg@hotmail.com

              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