By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,728 Members | 2,490 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,728 IT Pros & Developers. It's quick & easy.

Listbox, array, item deletion

P: n/a
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?

Thanks
Nov 3 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
It's not possible. All you can do is to create an new array(leaving the
deleted string) and assign it to the same old array variable/field.

Sweetiecakes wrote:
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?

Thanks
Nov 3 '08 #2

P: n/a
"Sweetiecakes" <x@x.comwrote in message
news:49***********************@news.fv.fi...
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?
Like Ashutosh said, there's no way to delete an arbitrary element from an
array. Next time consider using a List<stringinstead and it will handle
removing items for you.
Nov 3 '08 #3

P: n/a
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
Nov 3 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.