can't delete all items from CListBox
-
i've got this code from MSDN http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/\_mfc\_clistbox.3a3a.deleteitem.asp
for (int i=0;i < pmyListBox->GetCount();i++)
{
pmyListBox->DeleteString( i );
}but it doesn't work, it deletes almost every item, but not all of them. It doesn't delete all items on the list. Any ideas how to do that?
-
i've got this code from MSDN http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/\_mfc\_clistbox.3a3a.deleteitem.asp
for (int i=0;i < pmyListBox->GetCount();i++)
{
pmyListBox->DeleteString( i );
}but it doesn't work, it deletes almost every item, but not all of them. It doesn't delete all items on the list. Any ideas how to do that?
-
pmyListBox->ResetContent();
"No matter where you go, there your are." - Buckaroo Banzai
-pete
-
Sometimes, you need to see each item before deleting it (like to delete data associated with the item data pointer). The reason the original code didn't work was that deleting item 0 then moves all the other items up one. Then you delete item 1 and the same thing happens to items 2-n. You probably ended up with opnly about half the items deleted. The solution is to either go through the list backwards, or to make the loop like this:
while(lbx.GetCount()) { Data* p = (Data*)lbx.GetItemDataPtr(0); // do something with p; lbx.DeleteString(0); }
-
i've got this code from MSDN http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/\_mfc\_clistbox.3a3a.deleteitem.asp
for (int i=0;i < pmyListBox->GetCount();i++)
{
pmyListBox->DeleteString( i );
}but it doesn't work, it deletes almost every item, but not all of them. It doesn't delete all items on the list. Any ideas how to do that?
This is buggy - you want to do
DeleteString(0)
notDeleteString(i)
sinceGetCount()
will return you smaller values each time you callDeleteString()
. Or as has already been pointed out, you can just useResetContent()
. /ravi My new year's resolution: 2048 x 1536 Home | Articles | Freeware | Music ravib@ravib.com -
i've got this code from MSDN http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/\_mfc\_clistbox.3a3a.deleteitem.asp
for (int i=0;i < pmyListBox->GetCount();i++)
{
pmyListBox->DeleteString( i );
}but it doesn't work, it deletes almost every item, but not all of them. It doesn't delete all items on the list. Any ideas how to do that?
for(int i=0;iGetCount()-1;i++) msn:Flashcutreg@hotmail.com