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 deleting items

Listbox deleting items

Scheduled Pinned Locked Moved C#
helpquestion
8 Posts 3 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.
  • G Offline
    G Offline
    Gavin Coates
    wrote on last edited by
    #1

    I seem to be encountering difficulty while trying to delete an item from a listbox. The user selects an item from the listbox, then clicks a delete button. The button code is as follows: try { listBox1.Items.Remove(listBox1.SelectedItem); } catch(IndexOutOfRangeException ex) { MessageBox.Show("Could not delete the selected item", "Error", MessageBoxButtons.OK); } Now, if I select the first item in the list, and press delete, the exception IndexOutOfRange occurs, however the item is then successfully deleted. If I select any other item in the list and try to delete it, it works fine, no exception Why does deleting the first item cause an exception

    M 1 Reply Last reply
    0
    • G Gavin Coates

      I seem to be encountering difficulty while trying to delete an item from a listbox. The user selects an item from the listbox, then clicks a delete button. The button code is as follows: try { listBox1.Items.Remove(listBox1.SelectedItem); } catch(IndexOutOfRangeException ex) { MessageBox.Show("Could not delete the selected item", "Error", MessageBoxButtons.OK); } Now, if I select the first item in the list, and press delete, the exception IndexOutOfRange occurs, however the item is then successfully deleted. If I select any other item in the list and try to delete it, it works fine, no exception Why does deleting the first item cause an exception

      M Offline
      M Offline
      Mazdak
      wrote on last edited by
      #2

      I test your code and there is no problem with that. It works properly for all items. Whats your error message? Mazy No sig. available now.

      G 1 Reply Last reply
      0
      • M Mazdak

        I test your code and there is no problem with that. It works properly for all items. Whats your error message? Mazy No sig. available now.

        G Offline
        G Offline
        Gavin Coates
        wrote on last edited by
        #3

        i just get an "IndexOutOfRangeException" occuring. Im using Borland C# if thats any help?

        M 1 Reply Last reply
        0
        • G Gavin Coates

          i just get an "IndexOutOfRangeException" occuring. Im using Borland C# if thats any help?

          M Offline
          M Offline
          Mazdak
          wrote on last edited by
          #4

          Gavin Coates wrote: i just get an "IndexOutOfRangeException" occuring Every exeptions have a Message property, which is string contained a error string. Gavin Coates wrote: Borland C# Oh, I don't know about that. I haven't seen anybody use it here. Mazy No sig. available now.

          G 1 Reply Last reply
          0
          • M Mazdak

            Gavin Coates wrote: i just get an "IndexOutOfRangeException" occuring Every exeptions have a Message property, which is string contained a error string. Gavin Coates wrote: Borland C# Oh, I don't know about that. I haven't seen anybody use it here. Mazy No sig. available now.

            G Offline
            G Offline
            Gavin Coates
            wrote on last edited by
            #5

            error is: "Index was outside the bounds of the array"

            T 1 Reply Last reply
            0
            • G Gavin Coates

              error is: "Index was outside the bounds of the array"

              T Offline
              T Offline
              thomasa
              wrote on last edited by
              #6

              Try private void btnDelTheListBoxItem_Click(object sender, System.EventArgs e) { if(TheListBox.SelectedItem != null) { for(int i = 0; i < TheListBox.Items.Count; i++) { if(TheListBox.SelectedItem.Equals(TheListBox.Items[i])) { TheListBox.Items.RemoveAt(i); i = TheListBox.Items.Count; } } } } This will work

              G 1 Reply Last reply
              0
              • T thomasa

                Try private void btnDelTheListBoxItem_Click(object sender, System.EventArgs e) { if(TheListBox.SelectedItem != null) { for(int i = 0; i < TheListBox.Items.Count; i++) { if(TheListBox.SelectedItem.Equals(TheListBox.Items[i])) { TheListBox.Items.RemoveAt(i); i = TheListBox.Items.Count; } } } } This will work

                G Offline
                G Offline
                Gavin Coates
                wrote on last edited by
                #7

                ggthanks thomasa. I tried the code above, but again i get exactly the same results! I can delete any item in the listbox no problems, but when i try to delete the first item, it deletes, but causes an IndexOutOfRangeException!! I tried creating a whole new project with just a listbox, textbox add and delete button, and it worked perfectly, no errors at all. It just causes problems in my current project!!! Please help!

                T 1 Reply Last reply
                0
                • G Gavin Coates

                  ggthanks thomasa. I tried the code above, but again i get exactly the same results! I can delete any item in the listbox no problems, but when i try to delete the first item, it deletes, but causes an IndexOutOfRangeException!! I tried creating a whole new project with just a listbox, textbox add and delete button, and it worked perfectly, no errors at all. It just causes problems in my current project!!! Please help!

                  T Offline
                  T Offline
                  thomasa
                  wrote on last edited by
                  #8

                  Strange. You should sett up some breakpoints and find out what index the first Item in your listbox has. It should have index 0. It might be that you add the item to your listbox in a special way? I use: TheListBox.Items.Add("Some text");

                  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