Deleting multiple selected items in ListControl 
Author Message
 Deleting multiple selected items in ListControl

Hi,
  I am trying to delete rows in a CListCtrl in report form. I set the multiple selection option. It is not deleting all the selected items, instead it is deleting some of them and leaving the rest as it is. I am using this chunk of code

int nItem=0;
UINT nState = 0;
POSITION pos;
nItem = m_lst.GetSelectedCount();
pos = m_lst.GetFirstSelectedItemPosition();
while(pos)
{
        nItem = m_lst.GetNextSelectedItem(pos);
        m_lst.DeleteItem(nItem);

Quote:
}

     Can anyone tell what could be the problem.
Thanx in advance,
Kumar

-----------------** -- Posted from CodeGuru -- **-----------------
http://www.*-*-*.com/ ;  The website for Visual C++ programmers.



Mon, 03 Dec 2001 03:00:00 GMT  
 Deleting multiple selected items in ListControl
i guess you should have your loop like this

DeleteItem(pos);
pos--;//since deletion would shift the selection up
GetNext..(pos)

HTH
sCaLLioN


Quote:

> Hi,
>   I am trying to delete rows in a CListCtrl in report form. I set the

multiple selection option. It is not deleting all the selected items,
instead it is deleting some of them and leaving the rest as it is. I am
using this chunk of code
Quote:

> int nItem=0;
> UINT nState = 0;
> POSITION pos;
> nItem = m_lst.GetSelectedCount();
> pos = m_lst.GetFirstSelectedItemPosition();
> while(pos)
> {
>         nItem = m_lst.GetNextSelectedItem(pos);
> m_lst.DeleteItem(nItem);
> }

>      Can anyone tell what could be the problem.
> Thanx in advance,
> Kumar

> -----------------** -- Posted from CodeGuru -- **-----------------
> http://www.codeguru.com/    The website for Visual C++ programmers.



Mon, 03 Dec 2001 03:00:00 GMT  
 Deleting multiple selected items in ListControl
Yep, this is a problem. Since you are getting one element at a time and
deleting them, so nItem would keep on getting incremented, but in reality
what happens is nItem remains always the index of the first selected item.
This happens because when you delete the first selected item, the next
selected item would now become the first selected item, and so on...

Use the following code for deleting the selected items.

POSITION pos = 0;
int nItem = 0;
int nItemCount = m_lst.GetSelectedCount();
while(nItemCount--)
{
    pos = m_lst.GetFirstSelectedItemPosition();
    if (pos)    // Actually should be ASSERT(pos);
    {
        nItem = m_lst.GetNextSelectedItem(pos);
        m_lst.DeleteItem(nItem);
    }

Quote:
}

Of course, this is just one way of doing it. There are other ways to do the
same.

The POSITION-type of iterators are really painful when wrapped up...

Regards,
Vivek Iyer
Geometric, Pune-India

Quote:

>Hi,
>  I am trying to delete rows in a CListCtrl in report form. I set the

multiple selection option. It is not deleting all the selected items,
instead it is deleting some of them and leaving the rest as it is. I am
using this chunk of code
Quote:

>int nItem=0;
>UINT nState = 0;
>POSITION pos;
>nItem = m_lst.GetSelectedCount();
>pos = m_lst.GetFirstSelectedItemPosition();
>while(pos)
>{
>        nItem = m_lst.GetNextSelectedItem(pos);
> m_lst.DeleteItem(nItem);
>}

>     Can anyone tell what could be the problem.
>Thanx in advance,
>Kumar

>-----------------** -- Posted from CodeGuru -- **-----------------
>http://www.codeguru.com/    The website for Visual C++ programmers.



Tue, 04 Dec 2001 03:00:00 GMT  
 Deleting multiple selected items in ListControl
I tried sending this last week, but it never got through....

This is what I'm using to delete multiple items.... if you use it, let me
know if there are any problems with it.

POSITION pos = ListCtrl.GetFirstSelectedItemPosition();

 while (pos != NULL) {
    index = ListCtrl.GetNextSelectedItem(pos);
    ....

    pos = ListCtrl.GetFirstSelectedItemPosition();
 }


Quote:

> Hi,
>   I am trying to delete rows in a CListCtrl in report form. I set the

multiple selection option. It is not deleting all the selected items,
instead it is deleting some of them and leaving the rest as it is. I am
using this chunk of code
Quote:

> int nItem=0;
> UINT nState = 0;
> POSITION pos;
> nItem = m_lst.GetSelectedCount();
> pos = m_lst.GetFirstSelectedItemPosition();
> while(pos)
> {
>         nItem = m_lst.GetNextSelectedItem(pos);
> m_lst.DeleteItem(nItem);
> }

>      Can anyone tell what could be the problem.
> Thanx in advance,
> Kumar

> -----------------** -- Posted from CodeGuru -- **-----------------
> http://www.codeguru.com/    The website for Visual C++ programmers.



Fri, 07 Dec 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Deleting Selected items in CListViewCtrl

2. Multiple selected items in a listbox

3. Double-click multiple selected items

4. Multiple selected items drag image

5. Drag Image for Multiple selected Items

6. Multiple Selected Items Drag Image?

7. Selecting multiple items in list control

8. How to delete multiple items ?

9. Selecting Items from a multi select list box VC++ 6.0 on W2K

10. Count selected items of CListView while selecting

11. How To: Selected Item or Items in Web Browser (in designMode)

12. TreeView with multiple column and multiple select.

 

 
Powered by phpBB® Forum Software