I've got a little problem with multiple selection in a listview .
( application written in C : NO MFC !! ) .
I use the following code to retrieve which elements are selected :
////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////
multiple_sel_info.sel_cnt = ListView_GetSelectedCount(hWndListView3);
u = 0;
i = 0;
tmpcnt = cnt_RAM_blocks;
do
{
if
(LVIS_SELECTED==ListView_GetItemState(hWndListView3,i,LVIS_SELECTED))
{
if
(!rgRamListBlock[i].Parent)
{
ListView_DeleteItem(hWndListView3,i);
Adjust_blocks(i, 3,
cnt_RAM_blocks);
cnt_RAM_blocks--;
}
u++;
}
i++;
if (i > tmpcnt)
break;
}
while (u <=
multiple_sel_info.sel_cnt);
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////
The purpose of this piece of code is to delete all the items that are
selected : it searches
from the first index to the last which elements are selected. The selected
items must be removed.
Very strange is that if I use "ListView_GetSelectedCount( )" I get the
right number of selected elements, however it does not retrieve all of the
selected elements in the loop.
I've also tried to use "ListView_GetNextItem ()" but the result was even
worse.
If anyone knows what I'm doing wrong or knows a better solution please
contact me at :
Thanks a lot,
Dimitri.
Short answer -- you're modifying your index within the loop.
Longer answer below.
[I've reformatted the code to make it possible to read it -- please
be careful of deeply nested tabs when you post code!]
In <01bd2104$0da45060$e8c0...@BPSWFW371.BARCO.COM>, Degraeve Dimitri
<dimitri....@barco.com> wrote:
[...]
>multiple_sel_info.sel_cnt = ListView_GetSelectedCount(hWndListView3);
>u = 0; i = 0; tmpcnt = cnt_RAM_blocks;
>
>do {
> if (LVIS_SELECTED==ListView_GetItemState(hWndListView3,i,LVIS_SELECTED)) {
> if (!rgRamListBlock[i].Parent) {
> ListView_DeleteItem(hWndListView3,i);
> Adjust_blocks(i, 3,cnt_RAM_blocks);
> cnt_RAM_blocks--;
> }
> u++;
> }
> i++;
> if (i > tmpcnt)
> break;
>} while (u <= multiple_sel_info.sel_cnt);
>
>The purpose of this piece of code is to delete all the items that are
>selected : it searches from the first index to the last which elements
>are selected. The selected items must be removed.
>
>Very strange is that if I use "ListView_GetSelectedCount( )" I get the
>right number of selected elements, however it does not retrieve all of the
>selected elements in the loop.
If I'm reading this correctly, i is the offset of the item within the
list view, and tmpcnt is the total number of items in the list view
(it's hard to tell for sure -- you should use more descriptive variable
names). Looks to me like you're not adjusting i properly when you delete
a selected item, so you're skipping items.
So if you had a list:
Item A <selected>
Item B <selected>
Item C
You'd properly delete A, then increment i, determine that C isn't
selected (after deleting A, C is item at offset 1 in the list),
call ListView_GetSelectedCount on a nonexistent item at offset 2,
and exit the loop.
Since you're using offsets, you either have to adjust them when
the contents of the list changes, or delete items from the last
item in the list back to the beginning of the list.
Katy
MVP/VC++
--
Katy Mulvey k...@ormec.com
Software Development Engineer
ORMEC Systems http://www.ormec.com
>Hi,
>
> I've got a little problem with multiple selection in a listview .
>( application written in C : NO MFC !! ) .
>
>[cut]
>
>The purpose of this piece of code is to delete all the items that are
>selected : it searches
>from the first index to the last which elements are selected. The selected
>items must be removed.
>
You are deleting from top to bottom. When you delete a selected row
all the rows above that one drop down one, so when you check the
'next' row you are actually checking the one after... Delete from
bottom to top and you will avoid this problem.
Tony
--------------------------------------------------------------------------------------------
On the side of the software box, in the "System Requirements" section,
it said "Requires Windows 95 or better". So I installed Linux.
t...@netfusion.co.uk http://sale.netfusion.co.uk
Work: (+44) 161 278 2463 Mobile: (+44) 441 141 300
---------------------------------------------------------------------------------------------