Deleting a listbox item
-
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
-
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
In a
Listbox
, all items are stored in a 'collection'. You can access this collection usingmyListbox.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, andmyListBox.Items[myListBox.Items.Count - 1]
the last item. You can use themyListBox.Items.Remove()
andmyListBox.Items.RemoveAt()
methods to remove an item. Look in the documentation of theListBox
for further info. - Daniël PelsmaekerAs I said before: I never repeat myself!
-
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
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
-
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
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
-
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
-
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
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.