Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Deleting multiple selected items in a CListCtrl - PLEASE HELP!!

1,323 views
Skip to first unread message

Spike Pierson

unread,
May 1, 2001, 1:35:32 PM5/1/01
to
Hello All,

Q: What did the fish say when he hit a solid concrete wall?
A: "DAM"!!

Well I just hit another solid concrete wall, and I too am saying "DAMN"!! I
am developing an MFC Doc/View app which utilizes a CListCtrl to present a
list of items to the user. I want to give the user the ability to select
multiple items and delete them. My problem is that when I iterate through
the selected items from the top, and delete them (as shown in the loop
structure below), items get skipped over because their indices change when
an item is deleted.

// Begin code snippet

CListCtrl m_wndList;
...
...
POSITION posItem = m_wndList.GetFirstSelectedItemPosition();

while (posItem)
{
int nIndex = m_wndList.GetNextSelectedItem(posItem);
m_wndList.DeleteItem(nIndex);
}
// End code snippet

It seems like if I could iterate backwards, the problem would go away
because any previous index would be unaffected by DeleteItem(). If I could
start with the LAST selected item position and iterate with a get PREVIOUS
selected item method, then it would work fine. I have searched the
documentation, and I don't see any CListCtrl members that can facilitate
this. But certainly there must be a way to effectively accomplish this,
such as when one deletes multiple files in the right pane of Windows
Explorer.

Could somebody please provide me with some guidance here? I would be
forever grateful. Thanks in advance.


Spike Pierson

Klueless

unread,
May 1, 2001, 3:45:23 PM5/1/01
to

Delete backwards exactly as you proposed. The
only complication was that GetFirstSelectedItemPosition
and GetNextSelectedItem proceeded through selected
indices in the forwards direction while you need the indices
in the backwards direction. It happens, however, that
MFC implements all 3 functions

GetFirstSelectedItemPosition
GetNextSelectedItem
GetNextItem

as LVM_GETNEXTITEM messages to the list control
and if you look up the documentation for either
GetNextItem or LVM_GETNEXTITEM you will see that
GetNextItem is general enough that you can use it to
search backwards through the selections.


Jay Nabonne

unread,
May 4, 2001, 1:43:59 PM5/4/01
to
Since you're always deleting the item, simply keep going back to the start:
// Begin code snippet

CListCtrl m_wndList;
...
...
POSITION posItem;

while (posItem = m_wndList.GetFirstSelectedItemPosition())


{
int nIndex = m_wndList.GetNextSelectedItem(posItem);
m_wndList.DeleteItem(nIndex);
}
// End code snippet

Jay Nabonne
RightAgain, Inc.


"Spike Pierson" <SPie...@xostech.com> wrote in message
news:8uCH6.3362$e9.9...@news1.fdn.com...

Hal Rodgers

unread,
May 7, 2001, 12:10:30 PM5/7/01
to
One simple technique is to iterate backwards through the container:
for(int index = size -1, index >= 0, index--)
...
That way the invalid indexes are all in a part of the container you don't
care about. This also works well with
stl containers where deletion invalidates iterators.

"Jay Nabonne" <j...@rightagain.com> wrote in message
news:OZAEOHM1AHA.1608@tkmsftngp03...

0 new messages