Deleting multiple items in a listbox

Seb

Member
Joined
Nov 26, 2003
Messages
13
Im trying to make a button delete more than one item at the same time when they r all highlighted in a list box. Heres what I have so far:

Code:
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
 
Ive 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 youre looking for.
 
Yeh but cant you loop through the selecteditems collection and delete all items in that collection?
 
Code:
Dim item As Object

For Each item In LeftListBox.SelectedItems
  LeftListBox.Items.Remove(item)
Next
 
Code:
Dim item As Object

For Each item In LeftListBox.SelectedItems
  LeftListBox.Items.Remove(item)
Next

Doent 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: )
Code:
object[] aItems = new object[listBox1.SelectedItems.Count];
listBox1.SelectedItems.CopyTo(aItems, 0);

foreach(object oDelItem in aItems)
{
	listBox1.Items.Remove(oDelItem);
}
 
Cant you try and delete the last (or first) item of the collection until youve looped through it all?
eg (Psuedocode)
Code:
while selecteditems is not nothing
    selecteditems.remove(0)
loop
 
------------------- Also -------------------

Seb
Newcomer

Registered: Nov 2003
Location:
Posts: 7

Ive 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.
 
Code:
while selecteditems is not nothing
    selecteditems.remove(0)
loop

wont work because selecteditems doesnt contains a remove function.. And if it has it would be more logic that it would only deselect the items.
 
Code:
for(int i = this.fileList.SelectedItems.Count - 1; i >= 0; i--)
 this.fileList.Items.Remove(this.fileList.SelectedItems[0]);
 
Not to jump into the middle of this, but have you considered using a ListView as it does support removing multiple items?
 
Code:
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...
 
Back
Top