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. Deleting a listbox item

Deleting a listbox item

Scheduled Pinned Locked Moved C#
csharpquestionlearning
6 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.
  • J Offline
    J Offline
    Jason Ranin
    wrote on last edited by
    #1

    i am currently learning c#.net and am able to add items in a listbox via a control button. however, i have not been able to delete a specific listbox item using another control button (i.e. delete). can anyone give me a suggestion? thanks. Sincerely, Jason C. Ranin Software Quality Assurance

    U T 2 Replies Last reply
    0
    • J Jason Ranin

      i am currently learning c#.net and am able to add items in a listbox via a control button. however, i have not been able to delete a specific listbox item using another control button (i.e. delete). can anyone give me a suggestion? thanks. Sincerely, Jason C. Ranin Software Quality Assurance

      U Offline
      U Offline
      User 260964
      wrote on last edited by
      #2

      In a Listbox, all items are stored in a 'collection'. You can access this collection using myListbox.Items. Use an indexer (e.g. a number which specifies which items you want) to select a specific item to manipulate. For example, myListBox.Items[0] returns the first item, myListBox.Items[1] the second, and myListBox.Items[myListBox.Items.Count - 1] the last item. You can use the myListBox.Items.Remove() and myListBox.Items.RemoveAt() methods to remove an item. Look in the documentation of the ListBox for further info. - Daniël Pelsmaeker

      As I said before: I never repeat myself!

      1 Reply Last reply
      0
      • J Jason Ranin

        i am currently learning c#.net and am able to add items in a listbox via a control button. however, i have not been able to delete a specific listbox item using another control button (i.e. delete). can anyone give me a suggestion? thanks. Sincerely, Jason C. Ranin Software Quality Assurance

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

        This is an example where a listbox item is selected: private void btnDelSelectedItem_Click(object sender, System.EventArgs e) { if(this.TheListBox.SelectedItem != null) { for(int i = 0; i < this.TheListBox.Items.Count; i++) { if(TheListBox.SelectedItem.Equals(TheListBox.Items[i])) { TheListBox.Items.RemoveAt(i); i = TheListBox.Items.Count; } } } } This is probably not the simplest way to do it, but it works. I've used this in my code, because I needed to check what kinde of item it was that should be delited, and then make "somrthing happen" depending on the item and the items serounding it. Hope it helps Thomas

        J 1 Reply Last reply
        0
        • T thomasa

          This is an example where a listbox item is selected: private void btnDelSelectedItem_Click(object sender, System.EventArgs e) { if(this.TheListBox.SelectedItem != null) { for(int i = 0; i < this.TheListBox.Items.Count; i++) { if(TheListBox.SelectedItem.Equals(TheListBox.Items[i])) { TheListBox.Items.RemoveAt(i); i = TheListBox.Items.Count; } } } } This is probably not the simplest way to do it, but it works. I've used this in my code, because I needed to check what kinde of item it was that should be delited, and then make "somrthing happen" depending on the item and the items serounding it. Hope it helps Thomas

          J Offline
          J Offline
          Jason Ranin
          wrote on last edited by
          #4

          Thanks a million. however, there is one problem that i forgot to ask in conjunction with this previous issue. i have actually written each listbox items in a text file. the other part to deleting them from the listbox is to delete them from the text file also. would u happen to know how to approach this since I can't seem to fing the right way. thanks. Sincerely, Jason C. Ranin Software Quality Assurance

          T B 2 Replies Last reply
          0
          • J Jason Ranin

            Thanks a million. however, there is one problem that i forgot to ask in conjunction with this previous issue. i have actually written each listbox items in a text file. the other part to deleting them from the listbox is to delete them from the text file also. would u happen to know how to approach this since I can't seem to fing the right way. thanks. Sincerely, Jason C. Ranin Software Quality Assurance

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

            It depends how you save the data, if it is a XML document, I've heard that there is som methods that alow you to modefie singel lines in the file. The easy way, if there aren't to many items in the List, is to resave all the file. Hope it helps Thomas

            1 Reply Last reply
            0
            • J Jason Ranin

              Thanks a million. however, there is one problem that i forgot to ask in conjunction with this previous issue. i have actually written each listbox items in a text file. the other part to deleting them from the listbox is to delete them from the text file also. would u happen to know how to approach this since I can't seem to fing the right way. thanks. Sincerely, Jason C. Ranin Software Quality Assurance

              B Offline
              B Offline
              Bojan Rajkovic
              wrote on last edited by
              #6

              Well, it would be quite easy to remove the item using listBox1.Items.Remove(listBox1.SelectedItem) or something similar I believe. And after you have done that, why not just call a method that will rewrite your text file with the current listbox items..That would be the simplest approach in my opinion.

              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