CListCtrl DeleteItem
-
Hello! Is there someone here who can tel my why this is not working for me. I want to delete some selected items in a ListControl, But It only deletes a few of them not all of them??
POSITION p = m_ListControll.GetFirstSelectedItemPosition(); while (p) { m_ListControll.DeleteItem(m_ListControll.GetNextSelectedItem(p)); }
He believed the commercials that said drink beer, get laid but at closin' time he was alone, he didn't get paid. Last night his lover was a razorblade.. "Dan Reed song Mix it up"
What do you want to patch today? -
Hello! Is there someone here who can tel my why this is not working for me. I want to delete some selected items in a ListControl, But It only deletes a few of them not all of them??
POSITION p = m_ListControll.GetFirstSelectedItemPosition(); while (p) { m_ListControll.DeleteItem(m_ListControll.GetNextSelectedItem(p)); }
He believed the commercials that said drink beer, get laid but at closin' time he was alone, he didn't get paid. Last night his lover was a razorblade.. "Dan Reed song Mix it up"
What do you want to patch today?Look up GetNextSelectedItem() in MSDN.
-
Hello! Is there someone here who can tel my why this is not working for me. I want to delete some selected items in a ListControl, But It only deletes a few of them not all of them??
POSITION p = m_ListControll.GetFirstSelectedItemPosition(); while (p) { m_ListControll.DeleteItem(m_ListControll.GetNextSelectedItem(p)); }
He believed the commercials that said drink beer, get laid but at closin' time he was alone, he didn't get paid. Last night his lover was a razorblade.. "Dan Reed song Mix it up"
What do you want to patch today?I would have thought that
DeleteItem()
would have a very noticeable side-effect on the current selection. Consider assigning item data's to the list control entries and using that to identify candidates for deletion. /ravi Let's put "civil" back in "civilization" Home | Articles | Freeware | Music ravib@ravib.com -
I would have thought that
DeleteItem()
would have a very noticeable side-effect on the current selection. Consider assigning item data's to the list control entries and using that to identify candidates for deletion. /ravi Let's put "civil" back in "civilization" Home | Articles | Freeware | Music ravib@ravib.com -
I tryed that to i did not work!:confused: He believed the commercials that said drink beer, get laid but at closin' time he was alone, he didn't get paid. Last night his lover was a razorblade.. "Dan Reed song Mix it up"
What do you want to patch today?Try this:
// Assign the item data to each list control entry
for (long nItem=0; (nItem < m_listCtrl.GetItemCount()); nItem++)
m_listCtrl.SetItemData (nItem, nItem);// Get the indexes of selected items
CUIntArray selectedItems;
POSITION pos = m_listCtrl.GetFirstSelectedItemPosition();
while (pos != NULL) {
long nIndex = m_listCtrl.GetNextSelectedItem (pos);
selectedItems.Add (nIndex);
}// For each selected item...
for (nItem=0; (nItem < selectedItems.GetSize(); nItem++) {// Find the item in the list control... for (long nIndex=0; (nIndex < m\_listCtrl.GetItemCount(); nIndex++) if (m\_listCtrl.GetItemData (nIndex) == selectedItems.GetAt (nItem)) { // And delete it m\_listCtrl.DeleteItem (nIndex); break; }
}
/ravi Let's put "civil" back in "civilization" Home | Articles | Freeware | Music ravib@ravib.com
-
Try this:
// Assign the item data to each list control entry
for (long nItem=0; (nItem < m_listCtrl.GetItemCount()); nItem++)
m_listCtrl.SetItemData (nItem, nItem);// Get the indexes of selected items
CUIntArray selectedItems;
POSITION pos = m_listCtrl.GetFirstSelectedItemPosition();
while (pos != NULL) {
long nIndex = m_listCtrl.GetNextSelectedItem (pos);
selectedItems.Add (nIndex);
}// For each selected item...
for (nItem=0; (nItem < selectedItems.GetSize(); nItem++) {// Find the item in the list control... for (long nIndex=0; (nIndex < m\_listCtrl.GetItemCount(); nIndex++) if (m\_listCtrl.GetItemData (nIndex) == selectedItems.GetAt (nItem)) { // And delete it m\_listCtrl.DeleteItem (nIndex); break; }
}
/ravi Let's put "civil" back in "civilization" Home | Articles | Freeware | Music ravib@ravib.com
Thank you! I tryed something similar but I never used the CUIntArray. This works Thank you again! /Jarek He believed the commercials that said drink beer, get laid but at closin' time he was alone, he didn't get paid. Last night his lover was a razorblade.. "Dan Reed song Mix it up"
What do you want to patch today? -
Hello! Is there someone here who can tel my why this is not working for me. I want to delete some selected items in a ListControl, But It only deletes a few of them not all of them??
POSITION p = m_ListControll.GetFirstSelectedItemPosition(); while (p) { m_ListControll.DeleteItem(m_ListControll.GetNextSelectedItem(p)); }
He believed the commercials that said drink beer, get laid but at closin' time he was alone, he didn't get paid. Last night his lover was a razorblade.. "Dan Reed song Mix it up"
What do you want to patch today?You have to iterate the list first to get all the selected items positions. Once you have that list go through it backwards to delete all the indexes as deleting them in the order you receive them changes the indexes of the following items.
int count = m\_UsingList.GetSelectedCount(); // Get the list of selected items and remove them if (count > 0) { int \*pIndexes = new int\[count\]; int index = 0; POSITION pos = m\_UsingList.GetFirstSelectedItemPosition(); while (pos) { pIndexes\[index++\] = m\_UsingList.GetNextSelectedItem(pos); } // we have to remove the items from the list in reverse order // as removing one before a later index changes the order! for (index = count - 1 ; index >= 0 ; index--) { m\_UsingList.DeleteItem(pIndexes\[index\]); } delete \[\]pIndexes; pIndexes = NULL; // done! }
Roger Allen Sonork 100.10016 Death come early, death come late, It takes us all, there is no reason. For every purpose under heaven, To each a turn, to each a season. A time to weep and a time to sigh, A time to laugh and a time to cry, A time to be born and a time to die. Dust to dust and ashes to ashes, And so I end my song.
-
Hello! Is there someone here who can tel my why this is not working for me. I want to delete some selected items in a ListControl, But It only deletes a few of them not all of them??
POSITION p = m_ListControll.GetFirstSelectedItemPosition(); while (p) { m_ListControll.DeleteItem(m_ListControll.GetNextSelectedItem(p)); }
He believed the commercials that said drink beer, get laid but at closin' time he was alone, he didn't get paid. Last night his lover was a razorblade.. "Dan Reed song Mix it up"
What do you want to patch today?Here are two ways:
for (int x = 0; x < m\_list.GetItemCount(); x++) { int nItem = m\_list.GetNextItem(-1, LVNI\_SELECTED); BOOL bStatus = m\_list.DeleteItem(nItem); }
and
int nItem = m\_list.GetNextItem(-1, LVNI\_SELECTED); do { BOOL bStatus = m\_list.DeleteItem(nItem); nItem = m\_list.GetNextItem(nItem - 1, LVNI\_SELECTED); } while (nItem != -1);
The latter is a tad faster as it does not have to continually search from the beginning of the list for a match.
Five birds are sitting on a fence. Three of them decide to fly off. How many are left?
-
Here are two ways:
for (int x = 0; x < m\_list.GetItemCount(); x++) { int nItem = m\_list.GetNextItem(-1, LVNI\_SELECTED); BOOL bStatus = m\_list.DeleteItem(nItem); }
and
int nItem = m\_list.GetNextItem(-1, LVNI\_SELECTED); do { BOOL bStatus = m\_list.DeleteItem(nItem); nItem = m\_list.GetNextItem(nItem - 1, LVNI\_SELECTED); } while (nItem != -1);
The latter is a tad faster as it does not have to continually search from the beginning of the list for a match.
Five birds are sitting on a fence. Three of them decide to fly off. How many are left?
It works fine Thank you! I am suprised that there is so many ways to delete multiple files in a listbox, and I could not pick any of them :) Thats the cool way of programming sometimes you know so much and so litle! He believed the commercials that said drink beer, get laid but at closin' time he was alone, he didn't get paid. Last night his lover was a razorblade.. "Dan Reed song Mix it up"
What do you want to patch today? -
Hello! Is there someone here who can tel my why this is not working for me. I want to delete some selected items in a ListControl, But It only deletes a few of them not all of them??
POSITION p = m_ListControll.GetFirstSelectedItemPosition(); while (p) { m_ListControll.DeleteItem(m_ListControll.GetNextSelectedItem(p)); }
He believed the commercials that said drink beer, get laid but at closin' time he was alone, he didn't get paid. Last night his lover was a razorblade.. "Dan Reed song Mix it up"
What do you want to patch today?"DeleteItem" makes indices of all subsequent items decrease by one, so suppose the "next selected item" had index of 5, it could become 4 after a call of "DeleteItem". So you should do this instead:
m_ListControll.LockWindowUpdate(); POSITION p = m_ListControll.GetFirstSelectedItemPosition(); while (p) { m_ListControll.DeleteItem(m_ListControll.GetNextSelectedItem(p)); p = m_ListControll.GetFirstSelectedItemPosition(); } m_ListControll.UnlockWindowUpdate();
Simply keep deleting the first selected item until there's no more, you really don't need to make things complicated, such as establishing an additional array.