how to remove all selected items from CListCtrl

Hi
 what is wrong with following code:

POSITION pos = m_wndList.GetFirtsSelectedItem();

if (NULL == pos)
   return;

int selected_item;

while(pos)
{
    selected_item = m_wndList.GetNextSelectedItem(pos);
    m_wndList.DeleteItem(selected_item);
}

no all selected items are removed:(
how to solve this problem.

Regards, Robert


0
sftech1 (45)
9/29/2004 7:43:49 AM
vc.mfc 33608 articles. 0 followers. Follow

5 Replies
2683 Views

Similar Articles

[PageSpeed] 0

In article <#TOnnofpEHA.1712@tk2msftngp13.phx.gbl>,
 "RobertK" <sftech@interia.pl> wrote:

>  what is wrong with following code:
> 
> POSITION pos = m_wndList.GetFirtsSelectedItem();
> 
> if (NULL == pos)
>    return;
> 
> int selected_item;
> 
> while(pos)
> {
>     selected_item = m_wndList.GetNextSelectedItem(pos);
>     m_wndList.DeleteItem(selected_item);
> }
> 
> no all selected items are removed:(
> how to solve this problem.

Well, two things:

(a) You have a return statement in the middle of your code, which is 
almost always a bad idea. The loop should be written like this:

   POSITION pos;
   int      selected_item;

   pos = m_wndList.GetFirstSelectedItem();
   while (NULL != pos)
   {
      selected_item = m_wndList.GetNextSelectedItem(pos);
      m_wndList.DeleteItem(selected_item);
   }


(b) However, that won't work, because you're deleting items out from 
under your iterator. You need to traverse the list from the end back to 
the beginning. Unfortunately, MFC is brain-dead, and provides only a 
forward iterator (GetNextSelectedItem(); what you want is 
GetPrevSelectedItem(), which doesn't exist).

So you need to traverse the list control's list of selected items first 
in the forward direction, marking the items you want to delete, and then 
do a reverse traversal on your internal list (in this example maintained 
using STL's list class), actually deleting them. Something like:

   pos = m_wndList.GetFirstSelectedItem();
   while (NULL != pos)
   {
      selected_item = m_wndList.GetNextSelectedItem(pos);
      myActualItem = m_wndList.GetItemData(selected_item);
      myActualItem.MarkForDeletion();
   }

   iter = myInternalList.end();
   while (iter != myInternalList.begin())
   {
      iter--;

      if ((*iter)->ToBeDeleted())
      {
         m_wndList.DeleteItem(selected_item);
         delete (*iter);

         selected_item--;
      }
   }

Error-checking and other niceties left as an exercise for the reader.

-Mike
0
nomad3715 (29)
9/29/2004 8:50:54 AM
nomad wrote:

> In article <#TOnnofpEHA.1712@tk2msftngp13.phx.gbl>,
>  "RobertK" <sftech@interia.pl> wrote:
> 
> 
>> what is wrong with following code:
>>
>>POSITION pos = m_wndList.GetFirtsSelectedItem();
>>
>>if (NULL == pos)
>>   return;
>>
>>int selected_item;
>>
>>while(pos)
>>{
>>    selected_item = m_wndList.GetNextSelectedItem(pos);
>>    m_wndList.DeleteItem(selected_item);
>>}
>>
>>no all selected items are removed:(
>>how to solve this problem.
> 
> 
> Well, two things:
> 
> (a) You have a return statement in the middle of your code, which is 
> almost always a bad idea. The loop should be written like this:
> 
>    POSITION pos;
>    int      selected_item;
> 
>    pos = m_wndList.GetFirstSelectedItem();
>    while (NULL != pos)
>    {
>       selected_item = m_wndList.GetNextSelectedItem(pos);
>       m_wndList.DeleteItem(selected_item);
>    }
> 
> 
> (b) However, that won't work, because you're deleting items out from 
> under your iterator. You need to traverse the list from the end back to 
> the beginning. Unfortunately, MFC is brain-dead, and provides only a 
> forward iterator (GetNextSelectedItem(); what you want is 
> GetPrevSelectedItem(), which doesn't exist).
> 
> So you need to traverse the list control's list of selected items first 
> in the forward direction, marking the items you want to delete, and then 
> do a reverse traversal on your internal list (in this example maintained 
> using STL's list class), actually deleting them. Something like:
> 
>    pos = m_wndList.GetFirstSelectedItem();
>    while (NULL != pos)
>    {
>       selected_item = m_wndList.GetNextSelectedItem(pos);
>       myActualItem = m_wndList.GetItemData(selected_item);
>       myActualItem.MarkForDeletion();
>    }
> 
>    iter = myInternalList.end();
>    while (iter != myInternalList.begin())
>    {
>       iter--;
> 
>       if ((*iter)->ToBeDeleted())
>       {
>          m_wndList.DeleteItem(selected_item);
>          delete (*iter);
> 
>          selected_item--;
>       }
>    }
> 
> Error-checking and other niceties left as an exercise for the reader.
> 
> -Mike

Well, actually you could do it a little more easier:

int nCurrentItem = m_kSelList.GetItemCount();

while (nCurrentItem >= 0)
{
	if (m_kSelList.GetItemState(nCurrentItem, LVIS_SELECTED) & LVIS_SELECTED)
		m_kSelList.DeleteItem(nCurrentItem);
	nCurrentItem--;
}

or implement a
do { ... } while(--nCurrentItem >= 0);

Stefan
0
9/29/2004 12:30:24 PM
In article <#FvLx#hpEHA.132@TK2MSFTNGP14.phx.gbl>,
 Stefan Pascal <stefan.pascal@simbolsoft.XXX> wrote:

> Well, actually you could do it a little more easier:
> 
> int nCurrentItem = m_kSelList.GetItemCount();
> 
> while (nCurrentItem >= 0)
> {
> 	if (m_kSelList.GetItemState(nCurrentItem, LVIS_SELECTED) & LVIS_SELECTED)
> 		m_kSelList.DeleteItem(nCurrentItem);
> 	nCurrentItem--;
> }

Yeah, that's better.

It's still stupid that one has to do this oneself to implement a common 
operation.

-Mike
0
nomad3715 (29)
9/29/2004 12:39:06 PM
nomad wrote:

> In article <#FvLx#hpEHA.132@TK2MSFTNGP14.phx.gbl>,
>  Stefan Pascal <stefan.pascal@simbolsoft.XXX> wrote:
> 
> 
>>Well, actually you could do it a little more easier:
>>
>>int nCurrentItem = m_kSelList.GetItemCount();
>>
>>while (nCurrentItem >= 0)
>>{
>>	if (m_kSelList.GetItemState(nCurrentItem, LVIS_SELECTED) & LVIS_SELECTED)
>>		m_kSelList.DeleteItem(nCurrentItem);
>>	nCurrentItem--;
>>}
> 
> 
> Yeah, that's better.
> 
> It's still stupid that one has to do this oneself to implement a common 
> operation.
> 
> -Mike

I wouldn't bet my money on that deleting all selected items is a "common 
operation". Anyways, you can subclass the CListCtrl and implement this 
code in a function called DeleteSelectedItems, or even better, you could 
implement a DeleteItems(UINT nMask) where you would specify the mask 
items should have in order to be deleted.

Stefan
P.S.: Personally, i had never managed to use a ListView in my 
application and NOT subclassing it. It lacks a lot of things. But think 
of that as a "task to be completed by any Win32 [MFC] programmer" :)
0
9/29/2004 1:14:32 PM
In article <eFTYbXipEHA.1712@tk2msftngp13.phx.gbl>,
 Stefan Pascal <stefan.pascal@simbolsoft.XXX> wrote:

> P.S.: Personally, i had never managed to use a ListView in my 
> application and NOT subclassing it.

That, I think, says everything one needs to say about CListCtrl and 
CListView.

-Mike
0
nomad3715 (29)
9/29/2004 1:53:44 PM
Reply:

Similar Artilces:

No Labels for Assembly Items = Completely Unacceptable
The longer I use RMS, the more this bothers me. The Label Wizard even offers an Item Selection criteria option for Assembly Items. The trouble is that it doesn't work! It's apparent that design intent did exist for printing labels for assembly items. Riddle me this, Microsoft: how long will I wait before I get the functionality you so obviously intended? What is the point of even having an assembly item if I can't print a label for it? I beg you. Please. Pretty please with sugar on top. Give me labels for assembly items!!! My frustration runneth over, Tom -- The wor...

Remove Excel Icon ??
Hi, I would like to remove the possibility to export data in Excel. I think I already read it's impossible but I just want to confirm. Thanks, Sylvie Your correct, there is no supported way to do this in the current release of Microsoft CRM 1.2 -- John O'Donnell Microsoft CRM MVP http://www.microsoft.com/BusinessSolutions/Community/CRMFaqLanding.aspx "sylvie" <sylvie.mondoux@proximi-t.com> wrote in message news:09dd01c47b03$d3627560$a301280a@phx.gbl... > Hi, > > I would like to remove the possibility to export data in > Excel. I think I already rea...

selecting accounts for Annual Budget Report
The annual budget report doesn't seem to have the options that other reports do to select certain accounts and transaction types to exclude from the report. As a result the net of income v. expenses in this report is different than what I think it should be. Any recommendations? "Mike" <mmcdaniel1960@comcast.net> wrote in message news:1173221450.116950.135110@n33g2000cwc.googlegroups.com... > The annual budget report doesn't seem to have the options that other > reports do to select certain accounts and transaction types to exclude > from the report. As a...

Removal
How do I completely remove all traces of Outlook 2002 for a complete fresh reinstall. I can't find anything for '02. HELP!!!!!!!!!!!!!!!!! ...

Remove lines with +++
Is there anyway to find any line that has a + in it and delete that whole line?! I'd also like to do the same for *. I have a list of about 1300-1500 names and addresses. Some of them have a few +++ next to the name and some of them have a few *** next to the name. These were put there by the company to designate things. They need to be removed from the list, but it's a lost of work doing it one by one. You could apply an autofilter to the column with these characters in and from the pull-down select Custom - in the panel choose "Contains" (scroll down for this) the...

how do I remove spaces from cells that were pasted
I am pasting numbers into a spreadsheet, however its treating them as text because there is a space before the number. How do I remove the space so it treats it as a number ? I have used the =trim() function and its not working. Thanks for any help ! Hi! It sounds like you're copy/pasting from a website. Try this macro from David McRitchie. Look for TRIMALL, it's about half way down the page: http://www.mvps.org/dmcritchie/excel/join.htm Biff >-----Original Message----- >I am pasting numbers into a spreadsheet, however its treating them as text >because there is a...

How to Remove SRS
How to I remove or disable Site Replication Service (SRS) from my Exchange 2003? Thank You, Jason HI Remove the SRS by expanding the Tools node in ESM, right-clicking Site Replication Service, and clicking Delete "jpollock" wrote: > How to I remove or disable Site Replication Service (SRS) from my Exchange > 2003? > > Thank You, > > Jason > > And make sure you are doing it while logged in to the console of the Exchange server on which the SRS is running. -- Jim McBee Web: http://www.somorita.com Blog: http://mostlyexchange.blogspot.com &...

Not found error 204: item not in database
I created a work order for a customer and every time I bring it up , I get "Not found error 204: item not in database". then the work order comes up. I was wondering what is up with this? brent hi seems this error. the item which you used in work order has been deleted. Please check the printed work order and then go to store manager and find that it exists. Also "Brent" wrote: > I created a work order for a customer and every time I bring it up , I get > "Not found error 204: item not in database". then the work order comes up. > I was ...

Cannot Remove a program from Add Remove via remove.
I downloaded a program Fast At Last and it did not download properly and I could not remove it. Even System Restore did not remove it. The program is listed as 924PL32. Kept getting messages The feature you are trying to use is on a network resource that is unavailable. C:\dell\GC605. Seems Dell is the publisher but they would not give me free support on this issue. Any suggestions as to how I can remove the program? I believe it may be associated with spyware. When I ran spysweeper it removed some Rouge Security products but I still cannot remove this program. Cor...

Removing an item from menu
How can I remove an item from the menu and add some thing else. For instance, how can I remove inactive from the menu in contact page and put some other item with my own code behind it? You can hide the public views by simply creating a team (non used views or whatever you want to name it) and share the view to that team(done in the customize entity fields in the forms and views), this will make it a private view that only members of that team can see. You can also create new public or private views in the same place. "Amin" wrote: > How can I remove an item from the m...

Help on how to select one formula from a list of formulae
Tricky question, not sure if it's possible withou the use of a macro. What I want to do is have a list of formula in a table, eg numbered 1 - 10. I then want a cell to be able to take one of these formulae, and apply it to figures local to it. I want to be able to alternate which formula is looked up dynamically. Vlookup doesnt work as the formula are imported exactly as-is. I cant get indirect working with this as it imports 'text' only. Anybody have any other ideas how to do this. Cheers for any help Not sure if I understand you correctly but it seems like something that the...

allmost done with removing our 1st exchange 03 server
We are in the process of removing the 1st exchange server in our domain. Everything has been replicated over to a new server and all mailboxes have been moved. When we shut down the original server down, email still works great for 90% of the team. For the rest of us, when type the name of person to receive an email, outlook it still tries to resolve the name on the original server. I checked the profile on the users mailbox and it shows them pointing to the new server. Any suggestions would be appreciated. Try creating a new Outlook profile. Also, when you shut down you're...

remove Fax from address book?
Is there a way to stop showing Fax in the address book? I have a lot of fax numbers in my Contacts. I do not want to loose them - just not show them in the Address Book. thx Outlook considers fax numbers to be valid electronic addresses, since there are many client- and server-based components that can use such addresses. One method to hide fax numbers from the address book is to prefix the fax number with one or more letters (maybe B for business fax, H for home, O for other). If the fax number begins with a letter, Outlook won't show it in the address book. There are a couple of t...

Need to remove selected characters
I have 25000 cells with map coordinates in it in the following format: N34* 51.87' I need the data in the following format: N55 13.26 So basically I need to strip out the * and ' from each cell. Any simple way to do this? I sure would appreciate any assistance. Thank you -- Dale Hi! Select the range of cells in question. Goto Edit>Rplace Find what: ~* Replace with: nothing, leave this blank Replace All Repeat the process for the ' It's VERY important that you select the specific range before you attempt this!!!!!!! Biff "Natedanger" <Natedanger@d...

How to select other random cells
I have a 2 part question. I want to have a 1 question survey randomly filled out by 200 people. Column A lists the 1 questions. Columun b has ABCDE listed in cells B6 to b10. I would like 200 samples in columns c, d, e, etc. How do I set this up so that they randomly Coose B6 to B 10. Second part. On another spreadsheet, I have a similar situation, but the user has 10 answers to select from and I want them to randomly select all that apply. How would I do this? Thanks! ...

Remove deduction code from payroll stub
Is it possible to remove just a single payroll deduction code from a payroll stub using 'Employee Checks Stub on Top-D'? For example, an employee has three deductions: Insurance, 401k, Medical. Would it be possible then to show insurance and 401k but leave medical off the check stub for all employees who have medical? Thanks in advance for your help! I don't believe so because the deduction field is an array so you'd have to know which array value. Even if you did, I don't think you can use arrays in report restrictions. -- Charles Allen, MVP "drose03...

How to Remove Yahoo toolbar
I have three toolbars installed on IE8-Yahoo (to the right of the address box), and Google and AVG below the address box. I want to uninstall the Yahoo toolbar but how? It is not listed under 'Add/Remove' programs and there is a magnifying glass and arrow to the right of this toolbar that says 'Manage search providers' so I removed Yahoo toolbar from there but it is still showing. I uninstalled Yahoo messenger (since the toolbar was included during the Messenger install so it would be removed by deleting Messenger) but it is still there. I know that for AV...

Removing password protection on a sheet
A co-worker of mine protected a sheet in her workbook but lost th password. I help her with formula/script issues and I thought I coul simply copy the data on this sheet and paste it to another sheet to ge around it, but it won't let me select anything other than a few cell (based on the cells settings I'm sure). I know that Microsoft doesn' support removal of passwords when you don't know the password (make sense from a secuity standpoint) but was hoping someone could help m out here. She can remake it if need be but thought I'd ask ;) Thanks -- Otto.ts ------------...

Closing PO's shouldn't create GL batch for item already returned
I have an issue where an item was on PO for 240 units. It was over-received at 280 units. When the invoice arrived, they were billed for 240 units. Before this person matched the invoice, they questioned the receiver, did a physical inventory and found that they truely only received 240 units, not the 280 that was received. That person ran a Return WITHOUT credit for 40 units and posted it. Then they matched the invoice to the PO for 240 units as normal and posted that. Later that day the purchasing person noticed that the PO was not closed so she went into Edit Purchase orders and...

display mail item in window on top
I have suddenly started having the following problem when I try to view individual mail items... When I double-click on a mail item to view it, the item opens in a new window UNDERNEATH Outlook. To access it, I have to either double-click a second time (which brings the window to the front) or click on the portion of the mail window that is showing. I cannot find a way to change any of the settings so that the mail item will display on TOP. I have even gone as far as to completely reinstall Outlook. That actually did fix the problem -- for about 3 hours. Any suggestions? See anot...

Removing old computers connector exchange 2003
Hi, I have a new exc 2003 server that is working fine. i keep getting errors in the event log that it cannot connect to an old exchange server that was removed. for some reason, in the connectors, i can see two connectors that point for the old server. connector for ms scheduleplus(oldservername) connector for ms mail (oldservername) i cannot delete them from there. what can i do? Omer On 4 Oct 2006 23:43:14 -0700, "etgarnet" <etgarnet@zahav.net.il> wrote: >Hi, > >I have a new exc 2003 server that is working fine. >i keep getting errors in the event log that ...

Adding and removing removable drives
Hello NG, I wrote an FolderView ActiveX control. How can I receive a notification for adding or removing drives for example usb sticks or other media? And how can I receive a notification to refresh network shares? Speial thanks for help. Thomas Bendrich Is this what you are looking for DBT_DEVNODES_CHANGED. AliR. "Thomas Bendrich" <admin@thb-net.com> wrote in message news:u7Gb2wjuFHA.2312@TK2MSFTNGP14.phx.gbl... > Hello NG, > > I wrote an FolderView ActiveX control. How can I receive a notification for > adding or removing drives for example usb sticks or ...

Select and Copy data in column.
Anyone know of a way I can do the following. I have a column (B) on my spreadsheet with a given persons name in every other cell. eg. Col A Col B Col C xxxx Smith xxxx xxxx xxxx xxxx Jones xxxx xxxx xxxx xxxx Smith xxxx xxxx xxxx The row immediately underneath the name also contains data pertaining to the named person in the row above i.e in my example above row number 2 also pertains to the person Smith, row number 4 also pertains to the person Jones, etc, etc. I am trying to copy the persons name into the cell direc...

create a thin border around the selected cells
need information on excel 2003 Hi Chandra, on the Menu bar choose FORMAT | CELL | BORD hth regards from Brazil Marcelo "Chandra Lee" escreveu: > need information on excel 2003 ...

Deleting Messages in 'Deleted Items' Box in Outlook Express
I have XP home edition. Is there any way of deleting some messages in the 'Deleted Items' box other than individually selecting each one? I have lots of messages in there that I would like to temporarily keep and some I want to permanently discard. Is there some way of selecting whole blocks of messages from one person without individually selecting each message? Merryterry wrote: > I have XP home edition. Is there any way of deleting some messages in > the 'Deleted Items' box other than individually selecting each one? > > I have lots of messages in th...