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. Remove items from the list box in C# windows application

Remove items from the list box in C# windows application

Scheduled Pinned Locked Moved C#
csharphelpquestion
6 Posts 6 Posters 1 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.
  • Y Offline
    Y Offline
    yadlaprasad
    wrote on last edited by
    #1

    Hi, i am trying remove some items from the listbox in C# windows application. but it doesn't work for me. can you please help?? below is the code snippet

    foreach (object o in Lstservices.SelectedItems)
    {
    MessageBox.Show(Lstservices.SelectedItem.ToString());
    Lstservices.SelectedItems.Remove(Lstservices.SelectedItem);

            }
    

    fttyhtrhyfytrytrysetyetytesystryrty

    A N A H P 5 Replies Last reply
    0
    • Y yadlaprasad

      Hi, i am trying remove some items from the listbox in C# windows application. but it doesn't work for me. can you please help?? below is the code snippet

      foreach (object o in Lstservices.SelectedItems)
      {
      MessageBox.Show(Lstservices.SelectedItem.ToString());
      Lstservices.SelectedItems.Remove(Lstservices.SelectedItem);

              }
      

      fttyhtrhyfytrytrysetyetytesystryrty

      A Offline
      A Offline
      Abhinav S
      wrote on last edited by
      #2

      You are trying to remove items from the same listbox you are running your loop on - this is not allowed in C#. By looking at your code, it appears you are trying to empty your list box. Use the Clear method (on the listbox).

      1 Reply Last reply
      0
      • Y yadlaprasad

        Hi, i am trying remove some items from the listbox in C# windows application. but it doesn't work for me. can you please help?? below is the code snippet

        foreach (object o in Lstservices.SelectedItems)
        {
        MessageBox.Show(Lstservices.SelectedItem.ToString());
        Lstservices.SelectedItems.Remove(Lstservices.SelectedItem);

                }
        

        fttyhtrhyfytrytrysetyetytesystryrty

        N Offline
        N Offline
        nagendrathecoder
        wrote on last edited by
        #3

        yadlaprasad wrote:

        Lstservices.SelectedItems.Remove(Lstservices.SelectedItem);

        I don't think this'll work. Use this instead :

        Lstservices.Items.Remove(o);

        1 Reply Last reply
        0
        • Y yadlaprasad

          Hi, i am trying remove some items from the listbox in C# windows application. but it doesn't work for me. can you please help?? below is the code snippet

          foreach (object o in Lstservices.SelectedItems)
          {
          MessageBox.Show(Lstservices.SelectedItem.ToString());
          Lstservices.SelectedItems.Remove(Lstservices.SelectedItem);

                  }
          

          fttyhtrhyfytrytrysetyetytesystryrty

          A Offline
          A Offline
          Abdul Rahman Hamidy
          wrote on last edited by
          #4

          use bellow

          while (Lstservices.SelectedItems.Count>0)
          {
          Lstservices.Items.Remove(Lstservices.SelectedItem);
          }

          Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan

          1 Reply Last reply
          0
          • Y yadlaprasad

            Hi, i am trying remove some items from the listbox in C# windows application. but it doesn't work for me. can you please help?? below is the code snippet

            foreach (object o in Lstservices.SelectedItems)
            {
            MessageBox.Show(Lstservices.SelectedItem.ToString());
            Lstservices.SelectedItems.Remove(Lstservices.SelectedItem);

                    }
            

            fttyhtrhyfytrytrysetyetytesystryrty

            H Offline
            H Offline
            Henry Minute
            wrote on last edited by
            #5

            All that your code does is to remove an item from the SelectedItems collection of your ListBox (i.e. you are unselecting it), not from the ListBox itself. To remove/delete from the listbox replace

                        Lstservices.SelectedItems.Remove(Lstservices.SelectedItem);
            

            with

                        Lstservices.Items.Remove(Lstservices.SelectedItem);
            

            but bear in mind that, as someone else has mentioned, C# will complain if you try this whilst iterating over the collection.

            Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

            1 Reply Last reply
            0
            • Y yadlaprasad

              Hi, i am trying remove some items from the listbox in C# windows application. but it doesn't work for me. can you please help?? below is the code snippet

              foreach (object o in Lstservices.SelectedItems)
              {
              MessageBox.Show(Lstservices.SelectedItem.ToString());
              Lstservices.SelectedItems.Remove(Lstservices.SelectedItem);

                      }
              

              fttyhtrhyfytrytrysetyetytesystryrty

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              With foreach, you are not allowed to alter the collection; you could use a while loop instead.

              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