Reputation: 785
This works fine:
private void btnDelete_Click(object sender, EventArgs e)
{
for (int i = 0; i < listBox1.SelectedItems.Count; i++)
{
listBox1.Items.Remove(listBox1.SelectedItems[i].ToString());
i--;
}
}
But this doesn't work:
private void btnDelete_Click(object sender, EventArgs e)
{
listBox1.Items.Remove(listBox1.SelectedItems);
}
Why is the second btnDelete_Click
not working? I mean I select a line on my listBox1
with my mouse and then press the button.
Doesn't the .Remove
function recognize which line I selected? Even though I say .Remove(listBox1.SelectedItem)
, is it a must to have and selectedItem array? Isn't the word SelectedItems
self-explanatory? And since I clicked the line on my listBox1
with my mouse, can't the program or the IDE understand which line is selected? Why do I still have to use SelectedItems[i]
?
Upvotes: 2
Views: 909
Reputation: 31231
The reason the second example does not work is because you are trying to pass multiple items in the form of a collection to be removed at once.
To remove an item you need to do it one at a time, hence why you need a loop.
Also, might I suggest using a ListView
instead? I personally find them easier to use with many more options.
For example you could make your loop for a ListView
with just this
foreach (ListViewItem item in listView1.SelectedItems)
{
item.Remove();
}
If you are trying to remove only one item at a time that is selected then you do
listBox1.Items.Remove(listBox1.SelectedItem);
SelectedItem
rather than SelectedItems
, plural being the collection that it can't handle without a loop, singular being a single item that it can understand.
Upvotes: 1
Reputation: 4327
also change
for (int i = 0; i < listBox1.SelectedItems.Count; i++)
{
listBox1.Items.Remove(listBox1.SelectedItems[i].ToString());
i--;
}
to
for (int i = (listBox1.SelectedItems.Count - 1); i >= 0; i--)
{
listBox1.Items.Remove(listBox1.SelectedItems[i]);
}
and this should be
listBox1.Items.Remove(listBox1.SelectedItem);
Upvotes: 1
Reputation: 9249
ListBox.Items.Remove()
expects one single item to remove while ListBox.SelectedItems
returns a collection of items (SelectedObjectCollection
) - even if only one item is selected!
You could write an extension method for removing all selected items:
public static class ListBoxExtension
{
public static void RemoveSelectedItems(this ListBox source)
{
if(source==null) return;
while(source.SelectedItems.Count!=0)
{
source.Items.Remove(source.SelectedItems[0]);
}
}
}
This can be used like that:
this.listbox1.RemoveSelectedItems();
Upvotes: 1
Reputation: 40002
listBox1.SelectedItems
returns a collection of selected items. To remove an item, you can only pass in a single object in collection, using Remove()
, OR an index to an object in the collection with RemoveAt()
.
Remove an object:
listBox1.Items.Remove(listBox1.SelectedItem);
Remove an object at index:
listBox1.Items.RemoveAt(5);
There is no RemoveRange() method, which is what you are looking for.
Upvotes: 1
Reputation:
I think SelectedItems needs indeks number. Maybe that's why you can't delete any item.
Upvotes: 1