Results 1 to 7 of 7

Thread: remove items in list box by index.

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2018
    Location
    UK
    Posts
    49

    remove items in list box by index.

    Hi all,
    It is easy enough to add or remove items in a ListBox,but is it possible programatically to remove the items by referring to the index rather than the string(or number) item itself.
    Then once the first item has been removed(index 0) make the next item in the list(item 1) the new item 0. I think it might involve a loop.Is this possible.
    Hope this makes sense
    LL
    Last night I dreamt I went to Manderley again

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: remove items in list box by index.

    Code:
    ListBox1.Items.RemoveAt(index)

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: remove items in list box by index.

    Rather than Remove (which is based on the contents), you just need to use RemoveAt (which is based on the index):
    https://docs.microsoft.com/en-us/dot...tframework-4.8

    It will automatically alter the indexes of the other items, no looping required.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: remove items in list box by index.

    One thing to note: If you are going to remove more items than one, you will need a loop, and you will need to loop in reverse order. So, your loop would be:
    Code:
    For x = ListBox1.Items.count -1 To 0 Step -1
     'Remove the items here.
    Next
    If you went forwards, and removed the first item, then you'd end up running off the end of the list and getting an exception, because the loop would run for more iterations than you had items in the list, since you removed one.

    Also, you don't have to shift items around. They do that automatically. There aren't any gaps in the set of items, so if you remove one, you don't leave a gap, it just fills in.
    My usual boring signature: Nothing

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: remove items in list box by index.

    Here's how you work this sort of stuff out:

    1. Go to the .NET documentation home page here.
    2. Filter by API if desired.
    3. Enter the type or member you're interested in.
    4. Follow the relevant link from the results.
    5. If you are already familiar with adding and removing items then you know that it's done via the Items property, which is accessible from the properties list for the type.
    6. You can follow the link for the type of that property to find out all members of that type.
    7. We've already established that you are familiar with the Add and Remove methods of that type. I wonder what other methods it has.
    8. Lo and behold, the RemoveAt method is included in that list and described thusly:
    Removes the item at the specified index within the collection.
    Even for someone who doesn't know what they're looking for, that 's a couple of minutes worth of clicking, scrolling and reading at the most. This is a perfect example of why reading the relevant documentation should ALWAYS be your first option. Yes, I did follow my own advice when I was a beginner myself. That's how I answered many questions that other people posted when I first started here.

  6. #6

    Thread Starter
    Member
    Join Date
    Feb 2018
    Location
    UK
    Posts
    49

    Re: remove items in list box by index.

    Hi Paul,
    Great! works fine.Many thanks
    LL
    Last night I dreamt I went to Manderley again

  7. #7

    Thread Starter
    Member
    Join Date
    Feb 2018
    Location
    UK
    Posts
    49

    Re: remove items in list box by index.

    Hi Si,
    Three solutions here and all work
    Many thanks
    Last night I dreamt I went to Manderley again

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width