[RESOLVED] What wrong with this loop?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: [RESOLVED] What wrong with this loop?

  1. #1
    Join Date
    Jan 2008
    Location
    India
    Posts
    780

    [RESOLVED] What wrong with this loop?

    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.

    Code:
    //CListCtrl m_List;
    
    int count =m_List.GetItemCount();
    for (int i=0;i<count;i++)
    {
      //do msometing with item i
    
      m_List.DeleteItem(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.
    IN A DAY, WHEN YOU DON'T COME ACROSS ANY PROBLEMS - YOU CAN BE SURE THAT YOU ARE TRAVELLING IN A WRONG PATH

  2. #2
    Join Date
    Aug 2008
    Posts
    902

    Re: What wrong with this loop?

    When you delete an item, the structure of the list changes, so it makes no sense to index that way.

    Code:
    //CListCtrl m_List;
    
    int count = m_List.GetItemCount();
    for (int i=0; i<count; i++)
    {
      //do msometing with item 0
    
      m_List.DeleteItem(0);
    }
    or better yet, why not

    Code:
    //CListCtrl m_List;
    
    int count = m_List.GetItemCount();
    for (int i=0; i<count; i++)
    {
      //do msometing with item i
    }
    
    m_List.DeleteAllItems();
    Last edited by Chris_F; January 15th, 2011 at 12:45 AM.

  3. #3
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,060

    Re: What wrong with this loop?

    In addition to what Chris_F said, you can also loop over your elements in reverse order, from last to first which should also solve your problem.

  4. #4
    Join Date
    Jan 2008
    Location
    India
    Posts
    780

    Re: [RESOLVED] What wrong with this loop?

    thanks
    IN A DAY, WHEN YOU DON'T COME ACROSS ANY PROBLEMS - YOU CAN BE SURE THAT YOU ARE TRAVELLING IN A WRONG PATH

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center