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. General Programming
  3. C / C++ / MFC
  4. CListCtrl DeleteItem

CListCtrl DeleteItem

Scheduled Pinned Locked Moved C / C++ / MFC
question
10 Posts 6 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.
  • J Offline
    J Offline
    Jarek G
    wrote on last edited by
    #1

    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?

    D R R D A 5 Replies Last reply
    0
    • J Jarek G

      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?

      D Offline
      D Offline
      Daniel1324
      wrote on last edited by
      #2

      Look up GetNextSelectedItem() in MSDN.

      1 Reply Last reply
      0
      • J Jarek G

        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?

        R Offline
        R Offline
        Ravi Bhavnani
        wrote on last edited by
        #3

        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

        J 1 Reply Last reply
        0
        • R Ravi Bhavnani

          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

          J Offline
          J Offline
          Jarek G
          wrote on last edited by
          #4

          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?

          R 1 Reply Last reply
          0
          • J Jarek G

            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?

            R Offline
            R Offline
            Ravi Bhavnani
            wrote on last edited by
            #5

            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

            J 1 Reply Last reply
            0
            • R Ravi Bhavnani

              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

              J Offline
              J Offline
              Jarek G
              wrote on last edited by
              #6

              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?

              1 Reply Last reply
              0
              • J Jarek G

                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?

                R Offline
                R Offline
                Roger Allen
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • J Jarek G

                  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?

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  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?

                  J 1 Reply Last reply
                  0
                  • D David Crow

                    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?

                    J Offline
                    J Offline
                    Jarek G
                    wrote on last edited by
                    #9

                    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?

                    1 Reply Last reply
                    0
                    • J Jarek G

                      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?

                      A Offline
                      A Offline
                      Abin
                      wrote on last edited by
                      #10

                      "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.

                      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