Listbox, array, item deletion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sweetiecakes

    Listbox, array, item deletion

    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
  • Ashutosh Bhawasinka

    #2
    Re: Listbox, array, item deletion

    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

    Comment

    • Jeff Johnson

      #3
      Re: Listbox, array, item deletion

      "Sweetiecak es" <x@x.comwrote in message
      news:490f3dea$0 $25400$9b536df3 @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<stringinst ead and it will handle
      removing items for you.


      Comment

      • Peter Duniho

        #4
        Re: Listbox, array, item deletion

        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>.RemoveA t() 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

        Comment

        Working...