-
Apr 30th, 2019, 02:55 PM
#1
Thread Starter
Member
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
-
Apr 30th, 2019, 03:57 PM
#2
Re: remove items in list box by index.
Code:
ListBox1.Items.RemoveAt(index)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 30th, 2019, 04:00 PM
#3
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.
-
Apr 30th, 2019, 04:03 PM
#4
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
-
May 1st, 2019, 12:12 AM
#5
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.
-
May 1st, 2019, 03:41 AM
#6
Thread Starter
Member
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
-
May 1st, 2019, 03:45 AM
#7
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|