CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 1999
    Posts
    12

    List Control Selection Processing

    In a program I'm working on right now I need to select any number of rows in a list control and remove them when the user presses a button, and destroy a CStringList object associated with each item. The problem is the darned things won't go away. I'm using the following handler:

    void CIncludeDlg::OnRemdir()
    {
    CStringList* plSubDirs; // Pointer to a list of subdirectories
    int nItem = -1; // Item index in the control


    while ((nItem = m_lcIncludeList.GetNextItem(nItem, LVNI_ALL | LVNI_SELECTED)) != -1)
    {
    // Deallocate the associated stringlist object
    plSubDirs = (CStringList*) m_lcIncludeList.GetItemData(nItem);

    if (plSubDirs != NULL)
    delete plSubDirs;

    // Remove this item from the list control
    m_lcIncludeList.DeleteItem(nItem);
    }
    }



    When I attempt to remove more than one item from the list, only a few of them will actually disappear from the control. A run through the debugger seems to indicate that GetNextItem() is returning -1 somewhere in the middle of the selected items. Does anyone have an idea what's happening here?



  2. #2

    Re: List Control Selection Processing

    You have to delete the list control items starting at the last one, not at the first one! For example, given these items:

    Apple
    Banana
    Cherry

    If I want to delete items 0 and 1 (Apple and Banana), if I incorrectly start at the top of the list and delete item 0, Banana is now item 0, and Cherry is now item 1. When I next delete item 1, Cherry is incorrectly deleted!




    LA Leonard - Definitive Solutions, Inc.

  3. #3
    Join Date
    Apr 1999
    Posts
    12

    Re: List Control Selection Processing

    Oop.. Crud, you're right. I didn't anticipate that the index numbers would change during deletion. A simple little recursive function cleared up my problems. Thanks for your help, Leonard.


Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured