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. What wrong with this loop?

What wrong with this loop?

Scheduled Pinned Locked Moved C / C++ / MFC
question
8 Posts 7 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
    Le rner
    wrote on last edited by
    #1

    Hi all, i have an list ctrl with some items,i want to perform an action with each item of list ctrl than after performing action delete this item. i m using this.

    //CListCtrl m_List;

    int count =m_List.GetItemCount();
    for (int i=0;i

    here items are not fetched according to its serial like if firstly i m use item 1,than next item here is item 3 than 5 and so on..

    here all items are not processes by this loop

    please tell me what can i do for this.

    thanks in advance.

    A J C L C 6 Replies Last reply
    0
    • L Le rner

      Hi all, i have an list ctrl with some items,i want to perform an action with each item of list ctrl than after performing action delete this item. i m using this.

      //CListCtrl m_List;

      int count =m_List.GetItemCount();
      for (int i=0;i

      here items are not fetched according to its serial like if firstly i m use item 1,than next item here is item 3 than 5 and so on..

      here all items are not processes by this loop

      please tell me what can i do for this.

      thanks in advance.

      A Offline
      A Offline
      Andrew Brock
      wrote on last edited by
      #2

      When you delete item 0, every other item moves up 1, so what was item 1 is now item 0. Instead of using m_List.DeleteItem(i);, use m_List.DeleteItem(0); The same goes for getting the data or string from each of the items, you are always using the first item in the list

      1 Reply Last reply
      0
      • L Le rner

        Hi all, i have an list ctrl with some items,i want to perform an action with each item of list ctrl than after performing action delete this item. i m using this.

        //CListCtrl m_List;

        int count =m_List.GetItemCount();
        for (int i=0;i

        here items are not fetched according to its serial like if firstly i m use item 1,than next item here is item 3 than 5 and so on..

        here all items are not processes by this loop

        please tell me what can i do for this.

        thanks in advance.

        J Offline
        J Offline
        jixuduxing
        wrote on last edited by
        #3

        int count =m_List.GetItemCount(); for (int i=0;i<count;i++) { //do msometing with item i m_List.DeleteItem(0); } just like this。。

        A 1 Reply Last reply
        0
        • J jixuduxing

          int count =m_List.GetItemCount(); for (int i=0;i<count;i++) { //do msometing with item i m_List.DeleteItem(0); } just like this。。

          A Offline
          A Offline
          Andrew Brock
          wrote on last edited by
          #4

          jixuduxing wrote:

          //do msometing with item i

          Probably just a copy/paste mistake, but

          int count =m_List.GetItemCount();
          for (int i=0;i<count;i++) {
          //do something with item 0;

          m_List.DeleteItem(0);
          }

          1 Reply Last reply
          0
          • L Le rner

            Hi all, i have an list ctrl with some items,i want to perform an action with each item of list ctrl than after performing action delete this item. i m using this.

            //CListCtrl m_List;

            int count =m_List.GetItemCount();
            for (int i=0;i

            here items are not fetched according to its serial like if firstly i m use item 1,than next item here is item 3 than 5 and so on..

            here all items are not processes by this loop

            please tell me what can i do for this.

            thanks in advance.

            C Offline
            C Offline
            cp9876
            wrote on last edited by
            #5

            Or even easier to understand:

            //CListCtrl m_List;
            int count =m_List.GetItemCount();
            for (int i=0;i<count;i++)
            {
            //do someting with item i
            }
            m_List.DeleteAllItems();

            Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."

            1 Reply Last reply
            0
            • L Le rner

              Hi all, i have an list ctrl with some items,i want to perform an action with each item of list ctrl than after performing action delete this item. i m using this.

              //CListCtrl m_List;

              int count =m_List.GetItemCount();
              for (int i=0;i

              here items are not fetched according to its serial like if firstly i m use item 1,than next item here is item 3 than 5 and so on..

              here all items are not processes by this loop

              please tell me what can i do for this.

              thanks in advance.

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

              You should start using the MSDN documentation to learn about the classes you are implementing. Had you done so you would surely have found this[^].

              I must get a clever new signature for 2011.

              1 Reply Last reply
              0
              • L Le rner

                Hi all, i have an list ctrl with some items,i want to perform an action with each item of list ctrl than after performing action delete this item. i m using this.

                //CListCtrl m_List;

                int count =m_List.GetItemCount();
                for (int i=0;i

                here items are not fetched according to its serial like if firstly i m use item 1,than next item here is item 3 than 5 and so on..

                here all items are not processes by this loop

                please tell me what can i do for this.

                thanks in advance.

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #7

                Note that reversing the loop

                for (int i = count-1; i >= 0; i--)
                {
                //do msometing with item i

                m_List.DeleteItem(i);
                }

                would solve you problem.

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                1 Reply Last reply
                0
                • L Le rner

                  Hi all, i have an list ctrl with some items,i want to perform an action with each item of list ctrl than after performing action delete this item. i m using this.

                  //CListCtrl m_List;

                  int count =m_List.GetItemCount();
                  for (int i=0;i

                  here items are not fetched according to its serial like if firstly i m use item 1,than next item here is item 3 than 5 and so on..

                  here all items are not processes by this loop

                  please tell me what can i do for this.

                  thanks in advance.

                  Y Offline
                  Y Offline
                  yu jian
                  wrote on last edited by
                  #8
                  int count = m_List.GetItemCount();
                  for (int i = count - 1;i >= 0; i--)
                  {  
                    //do msometing with item i  
                    m_List.DeleteItem(i);
                  }  
                  

                  if you delete item from a list, you should delete it from the last to the first.

                  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