Click to See Complete Forum and Search --> : List Control Selection Processing


Gamble
April 10th, 1999, 03:49 AM
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?

LALeonard
April 11th, 1999, 02:59 PM
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.

Gamble
April 11th, 1999, 08:43 PM
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.