Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. C / C++ / MFC
  3. CListCtrl - Delete Selected Items???

CListCtrl - Delete Selected Items???

Scheduled Pinned Locked Moved C / C++ / MFC
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Ryan Baillargeon
    wrote on last edited by
    #1

    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.

    A 1 Reply Last reply
    0
    • R Ryan Baillargeon

      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.

      A Offline
      A Offline
      AlexMarbus
      wrote on last edited by
      #2

      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.

      R 2 Replies Last reply
      0
      • A AlexMarbus

        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.

        R Offline
        R Offline
        Ryan Baillargeon
        wrote on last edited by
        #3

        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.

        M 1 Reply Last reply
        0
        • A AlexMarbus

          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.

          R Offline
          R Offline
          Ryan Baillargeon
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • R Ryan Baillargeon

            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.

            M Offline
            M Offline
            Michael Dunn
            wrote on last edited by
            #5

            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~!

            R F 2 Replies Last reply
            0
            • M Michael Dunn

              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~!

              R Offline
              R Offline
              Ryan Baillargeon
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • M Michael Dunn

                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~!

                F Offline
                F Offline
                Frank Deo
                wrote on last edited by
                #7

                Mike, Can you give a bit more info regarding the "Snazzier way to fix". I need to implement this and i'm having the same problems. Thanks, Frank

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups