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. Delete all items of a CListBox

Delete all items of a CListBox

Scheduled Pinned Locked Moved C / C++ / MFC
question
6 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.
  • L Offline
    L Offline
    llp00na
    wrote on last edited by
    #1

    I am trying to delete all items of a CListBox However, only the top item is removed once calling this routine, How do I delete everything ? Thanks numOfItems= listBox.GetCount(); for (int i =0; i<=numOfItems; i++){ lisBox.DeleteString(i); }

    llp00na

    C S 2 Replies Last reply
    0
    • L llp00na

      I am trying to delete all items of a CListBox However, only the top item is removed once calling this routine, How do I delete everything ? Thanks numOfItems= listBox.GetCount(); for (int i =0; i<=numOfItems; i++){ lisBox.DeleteString(i); }

      llp00na

      C Offline
      C Offline
      Code o mat
      wrote on last edited by
      #2

      CListBox::ResetContent[^]. Btw there's also a fundamantal flaw in your method. Consider what your loop does: 1. i becomes 0, the very first ("zeroth") item is deleted from your list, this will raise all other items 1 place, so 0 goes away, 1st becomes the 0th, 2nd becomes 1st, 3rd becomes 2nd and so on... 2. i now becomes 1, the 1st item in your list is deleted, and the ones under it jump up one place, so the 0th item is untouched, the 1st is deleted, 2nd becomes 1st, 3->2, 4->3 ... 3. i now becomes 2, 2nd item is deleted, so 0th is untouched, 1st is untouched, 2nd is deleted, 3rd becomes 2bd, 4->3 and so on... Get the problem?

      > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Life: great graphics, but the gameplay sux. <

      L 1 Reply Last reply
      0
      • C Code o mat

        CListBox::ResetContent[^]. Btw there's also a fundamantal flaw in your method. Consider what your loop does: 1. i becomes 0, the very first ("zeroth") item is deleted from your list, this will raise all other items 1 place, so 0 goes away, 1st becomes the 0th, 2nd becomes 1st, 3rd becomes 2nd and so on... 2. i now becomes 1, the 1st item in your list is deleted, and the ones under it jump up one place, so the 0th item is untouched, the 1st is deleted, 2nd becomes 1st, 3->2, 4->3 ... 3. i now becomes 2, 2nd item is deleted, so 0th is untouched, 1st is untouched, 2nd is deleted, 3rd becomes 2bd, 4->3 and so on... Get the problem?

        > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Life: great graphics, but the gameplay sux. <

        L Offline
        L Offline
        llp00na
        wrote on last edited by
        #3

        Great answer Thanks for the clarification. Looks like i can not think anymore :doh:

        llp00na

        C 1 Reply Last reply
        0
        • L llp00na

          Great answer Thanks for the clarification. Looks like i can not think anymore :doh:

          llp00na

          C Offline
          C Offline
          Code o mat
          wrote on last edited by
          #4

          Yourwelcome. :) If you want to stick to the for loop, try movings in reverse, so something like:

          for (int I = Count - 1; I >= 0; I--)
          listbox.DeleteString(I);

          or simply do this:

          for (int I = 0; I < Count; I++)
          listbox.DeleteString(0);

          > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Life: great graphics, but the gameplay sux. <

          L 1 Reply Last reply
          0
          • C Code o mat

            Yourwelcome. :) If you want to stick to the for loop, try movings in reverse, so something like:

            for (int I = Count - 1; I >= 0; I--)
            listbox.DeleteString(I);

            or simply do this:

            for (int I = 0; I < Count; I++)
            listbox.DeleteString(0);

            > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Life: great graphics, but the gameplay sux. <

            L Offline
            L Offline
            llp00na
            wrote on last edited by
            #5

            Awesome ideas Thanks

            llp00na

            1 Reply Last reply
            0
            • L llp00na

              I am trying to delete all items of a CListBox However, only the top item is removed once calling this routine, How do I delete everything ? Thanks numOfItems= listBox.GetCount(); for (int i =0; i<=numOfItems; i++){ lisBox.DeleteString(i); }

              llp00na

              S Offline
              S Offline
              surya9812
              wrote on last edited by
              #6

              use this: m_listBox.resetContent(); Thanks Surya

              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