Seb Posted December 3, 2003 Posted December 3, 2003 I'm trying to make a button delete more than one item at the same time when they r all highlighted in a list box. Here's what I have so far: Private Sub DelButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DelButton.Click If LeftListBox.SelectedItems.Count <> 0 Then LeftListBox.Items.Remove(LeftListBox.SelectedItem) LeftTextBox.Focus() EnableDisableButtons() End If If RightListBox.SelectedItems.Count <> 0 Then RightListBox.Items.Remove(RightListBox.SelectedItem) LeftTextBox.Focus() EnableDisableButtons2() End If End Sub Cheers Quote
samsmithnz Posted December 3, 2003 Posted December 3, 2003 I've used the list box quite a but now, and i believe that the listbox.items.selecteditems() collection contains all the selected items, and may be what you're looking for. Quote Thanks Sam http://www.samsmith.co.nz
Seb Posted December 3, 2003 Author Posted December 3, 2003 I've tried that but it only deletes one at a time in either list box. Quote
Seb Posted December 5, 2003 Author Posted December 5, 2003 HELP NEEDED!! Anyone else have any ideas? Quote
samsmithnz Posted December 5, 2003 Posted December 5, 2003 Yeh but can't you loop through the selecteditems collection and delete all items in that collection? Quote Thanks Sam http://www.samsmith.co.nz
Seb Posted December 5, 2003 Author Posted December 5, 2003 How do I loop through the 'selecteditems' collection? Quote
*Experts* Bucky Posted December 5, 2003 *Experts* Posted December 5, 2003 Dim item As Object For Each item In LeftListBox.SelectedItems LeftListBox.Items.Remove(item) Next Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
TheNaughty Posted February 12, 2004 Posted February 12, 2004 Dim item As Object For Each item In LeftListBox.SelectedItems LeftListBox.Items.Remove(item) Next Doen't works in C# because the SelectedItems collection changes while looping through wich causes an error, anybody knows how to do it with a short code?? (The only working way i know right know is: ) object[] aItems = new object[listBox1.SelectedItems.Count]; listBox1.SelectedItems.CopyTo(aItems, 0); foreach(object oDelItem in aItems) { listBox1.Items.Remove(oDelItem); } Quote
samsmithnz Posted February 12, 2004 Posted February 12, 2004 Can't you try and delete the last (or first) item of the collection until you've looped through it all? eg (Psuedocode) while selecteditems is not nothing selecteditems.remove(0) loop Quote Thanks Sam http://www.samsmith.co.nz
Jay1b Posted February 12, 2004 Posted February 12, 2004 ------------------- Also ------------------- Seb Newcomer Registered: Nov 2003 Location: Posts: 7 I've tried that but it only deletes one at a time in either list box. 12-03-2003 06:14 PM ------------AND---------------------------------------- Seb Newcomer Registered: Nov 2003 Location: Posts: 7 HELP NEEDED!! Anyone else have any ideas? 12-05-2003 06:33 PM ------------------------------------------------------------ 19 minutes, isnt exactly a long time to leave it before you bump a thread. Leave a day or two in future, give people a chance to read your 'demands'. Everyone here is very helpful, and will help you if they can - i have been helped to no-end myself. Just dont take the piss out of them. Quote
TheNaughty Posted February 12, 2004 Posted February 12, 2004 while selecteditems is not nothing selecteditems.remove(0) loop won't work because selecteditems doesn't contains a remove function.. And if it has it would be more logic that it would only deselect the items. Quote
sho Posted February 12, 2004 Posted February 12, 2004 for(int i = this.fileList.SelectedItems.Count - 1; i >= 0; i--) this.fileList.Items.Remove(this.fileList.SelectedItems[0]); Quote
*Experts* DiverDan Posted February 12, 2004 *Experts* Posted February 12, 2004 Not to jump into the middle of this, but have you considered using a ListView as it does support removing multiple items? Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
TheNaughty Posted February 12, 2004 Posted February 12, 2004 for(int i = listBox1.SelectedItems.Count - 1; i >= 0; i--)listBox1.Items.Remove(listBox1.SelectedItems[0]); Thanks sho, that was the code i was lookin for.. After all it is so simple i should have thought it myself... Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.