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#
  4. ListBox selection removal

ListBox selection removal

Scheduled Pinned Locked Moved C#
questionhelp
3 Posts 2 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.
  • P Offline
    P Offline
    Pyro Joe
    wrote on last edited by
    #1

    yeah, I have a listBox that the user can select multiple items in the list, how do I go about removing the selected items? I tried so many things, but I haven't had any success, I'm thankful for any help. Stephen

    L 1 Reply Last reply
    0
    • P Pyro Joe

      yeah, I have a listBox that the user can select multiple items in the list, how do I go about removing the selected items? I tried so many things, but I haven't had any success, I'm thankful for any help. Stephen

      L Offline
      L Offline
      Luis Alonso Ramos
      wrote on last edited by
      #2

      Remove the selected items from the list?

      for(int i = lstBox.SelectedItems.Count; i >= 0; i--)
          lstBox.Items.RemoveAt(i);
      

      You have to do it bottom-up. If you do top-down, when you remove an item on the top, the index of the items below it change. You could also do this:

      while(lstBox.SelectedItems.Count > 0)
          lstBox.Items.Remove(lstBox.SelectedItems[0]);
      

      but I think the first way is easier to understand. I hope this helps! -- LuisR


      Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!

      The amount of sleep the average person needs is five more minutes. -- Vikram A Punathambekar, Aug. 11, 2005

      P 1 Reply Last reply
      0
      • L Luis Alonso Ramos

        Remove the selected items from the list?

        for(int i = lstBox.SelectedItems.Count; i >= 0; i--)
            lstBox.Items.RemoveAt(i);
        

        You have to do it bottom-up. If you do top-down, when you remove an item on the top, the index of the items below it change. You could also do this:

        while(lstBox.SelectedItems.Count > 0)
            lstBox.Items.Remove(lstBox.SelectedItems[0]);
        

        but I think the first way is easier to understand. I hope this helps! -- LuisR


        Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!

        The amount of sleep the average person needs is five more minutes. -- Vikram A Punathambekar, Aug. 11, 2005

        P Offline
        P Offline
        Pyro Joe
        wrote on last edited by
        #3

        thanks, that's it!

        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