On Mon, 03 Nov 2008 10:07:37 -0800, Sweetiecakes <x@x.comwrote:
Hi
I have an string[] array containing file paths. I have added them to a
ListBox with AddRange();
When I delete an item from the ListBox, how can I delete the same item
from the original array?
To match the ListBox semantics, it would be better to store your objects
in a List<Trather than an array. Then, when you call List<T>.Remove()
to remove a specific object, it will automatically find the first matching
object in the List<T>, remove it without leaving a gap in the collection.
Even better is that, as long as you _always_ add or remove elements from
the List<Tin the same way as they are added or removed to the ListBox,
you can just use the item index from the ListBox as the index in the
List<Tand then you can call List<T>.RemoveAt() instead of making the
collection look for the object each time.
You can implement the same basic functionality using an array, but you'll
either have to use IndexOf() to look for the element, or you'll have to
shift down all the elements after the one you're removing so that the
indices continue to match the ListBox indices.
Pete