CListCtrl - Delete Selected Items???
-
Maybe its 3:30 and My Head is Not Straight, But Im having trouble Figureing this one out. This is my First VC++ App and things are going quite well. ;P My Problem is I have a CListCtrl that is representing "Tasks" in a Queue. I would like the USer to Be Able to Delete any number of Selected Items in the Queue. So if they Select Five Items and Click "Remove" those Items and Their Corresponding Data will be deleted. My problem is Navigating Through the Selected Item List. In Other CListCtrls I use I use the Same Method of moving through the Item List with no Problem. Whats the best Method for Deleteing Items from a CListCtrl that are Selected. GetNExtSelected or using the LVIS_SELECTED mask to identify selected Items and remove them? quick sample code would be appreciated. recurse.org - Recusion For the Rest Of Us.
-
Maybe its 3:30 and My Head is Not Straight, But Im having trouble Figureing this one out. This is my First VC++ App and things are going quite well. ;P My Problem is I have a CListCtrl that is representing "Tasks" in a Queue. I would like the USer to Be Able to Delete any number of Selected Items in the Queue. So if they Select Five Items and Click "Remove" those Items and Their Corresponding Data will be deleted. My problem is Navigating Through the Selected Item List. In Other CListCtrls I use I use the Same Method of moving through the Item List with no Problem. Whats the best Method for Deleteing Items from a CListCtrl that are Selected. GetNExtSelected or using the LVIS_SELECTED mask to identify selected Items and remove them? quick sample code would be appreciated. recurse.org - Recusion For the Rest Of Us.
Assuming m_ctrlTaskList is the Listcontrol you use:
POSITION pos = m_ctrlTaskList.GetFirstSelectedItemPosition();
while (pos != NULL)
{
int nPosition = m_ctrlTaskList.GetNextSelectedItemPosition(pos);
m_ctrlTaskList.DeleteItem(nPosition);
}HTH -- Alex Marbus www.marbus.net But then again, I could be wrong.
-
Assuming m_ctrlTaskList is the Listcontrol you use:
POSITION pos = m_ctrlTaskList.GetFirstSelectedItemPosition();
while (pos != NULL)
{
int nPosition = m_ctrlTaskList.GetNextSelectedItemPosition(pos);
m_ctrlTaskList.DeleteItem(nPosition);
}HTH -- Alex Marbus www.marbus.net But then again, I could be wrong.
Thats what my code looks like exactly except for a TRACE I did on Position to Check for NULL. However, I did use your code exactly and my problem still remains. I am only deleteing every other Item that is selected, SO if I am deleteing item 123456 which are all selected using that code above leaves 246 intact and still selected. Am I a BOOB? This is irking me especially since its one of my last bugs, and I cant implement anything else until this is solved. Here Ill even copy out my code for you. I really need help on this. void CChildView::OnRemItems() { POSITION pos = m_SpewWnd.QueueView.GetFirstSelectedItemPosition(); while (pos != NULL) { int nItem=m_SpewWnd.QueueView.GetNextSelectedItem(pos); m_SpewWnd.QueueView.DeleteItem(nItem); } Invalidate(); } Maybe you could also tell me how to get that code selected while posting.. =) recurse.org - Recusion For the Rest Of Us.
-
Assuming m_ctrlTaskList is the Listcontrol you use:
POSITION pos = m_ctrlTaskList.GetFirstSelectedItemPosition();
while (pos != NULL)
{
int nPosition = m_ctrlTaskList.GetNextSelectedItemPosition(pos);
m_ctrlTaskList.DeleteItem(nPosition);
}HTH -- Alex Marbus www.marbus.net But then again, I could be wrong.
Okay, I got it working, BUt this is how.. good old recursion. All I did was read to the end of the selected list and then recursively delete from there.. heres the code: Does anyone have a better IDEA ? void CChildView::OnRemItems() { POSITION pos = m_SpewWnd.QueueView.GetFirstSelectedItemPosition(); DelQ(pos); Invalidate(); } void CChildView::DelQ(POSITION pos) { while (pos != NULL) { int nItem=m_SpewWnd.QueueView.GetNextSelectedItem(pos); DelQ(pos); m_SpewWnd.QueueView.DeleteItem(nItem); } } Is this the asinine way to do this? I like recursion But I dont think MS had this in Mind at all. recurse.org - Recusion For the Rest Of Us.
-
Thats what my code looks like exactly except for a TRACE I did on Position to Check for NULL. However, I did use your code exactly and my problem still remains. I am only deleteing every other Item that is selected, SO if I am deleteing item 123456 which are all selected using that code above leaves 246 intact and still selected. Am I a BOOB? This is irking me especially since its one of my last bugs, and I cant implement anything else until this is solved. Here Ill even copy out my code for you. I really need help on this. void CChildView::OnRemItems() { POSITION pos = m_SpewWnd.QueueView.GetFirstSelectedItemPosition(); while (pos != NULL) { int nItem=m_SpewWnd.QueueView.GetNextSelectedItem(pos); m_SpewWnd.QueueView.DeleteItem(nItem); } Invalidate(); } Maybe you could also tell me how to get that code selected while posting.. =) recurse.org - Recusion For the Rest Of Us.
I am only deleteing every other Item that is selected, SO if I am deleteing item 123456 which are all selected using that code above leaves 246 intact and still selected. That's because when you delete item 1, the result is that 12345 are selected (2-6 shift down to 1-5), so the next time through your loop you skip the new item 1, item 2 (previously 3) gets deleted, and so on. Easy way to fix: In your loop, get the first selected item and delete it. Then get the first selected item again and delete it. Repeat until there are no more selected items. This may be slow if your list is big, though. Snazzier way to fix: Loop thru and make a list of the indexes of all selected items. Then delete them working from the highest index to the lowest. --Mike-- http://home.inreach.com/mdunn/ All your base are belong to ME~!
-
I am only deleteing every other Item that is selected, SO if I am deleteing item 123456 which are all selected using that code above leaves 246 intact and still selected. That's because when you delete item 1, the result is that 12345 are selected (2-6 shift down to 1-5), so the next time through your loop you skip the new item 1, item 2 (previously 3) gets deleted, and so on. Easy way to fix: In your loop, get the first selected item and delete it. Then get the first selected item again and delete it. Repeat until there are no more selected items. This may be slow if your list is big, though. Snazzier way to fix: Loop thru and make a list of the indexes of all selected items. Then delete them working from the highest index to the lowest. --Mike-- http://home.inreach.com/mdunn/ All your base are belong to ME~!
The post Below this describes how to do this Task Recursively, but that has a lot of overhead if your list of selected Items is really large. I figured out the "mIchael Dunn" method sometime this morning and that works very well. Using GetFirstSelected() over and over. The List Method is probably the best way to go however. Thanks EVERYONE. recurse.org - Recusion For the Rest Of Us.
-
I am only deleteing every other Item that is selected, SO if I am deleteing item 123456 which are all selected using that code above leaves 246 intact and still selected. That's because when you delete item 1, the result is that 12345 are selected (2-6 shift down to 1-5), so the next time through your loop you skip the new item 1, item 2 (previously 3) gets deleted, and so on. Easy way to fix: In your loop, get the first selected item and delete it. Then get the first selected item again and delete it. Repeat until there are no more selected items. This may be slow if your list is big, though. Snazzier way to fix: Loop thru and make a list of the indexes of all selected items. Then delete them working from the highest index to the lowest. --Mike-- http://home.inreach.com/mdunn/ All your base are belong to ME~!