Removing items from binding list

paulthepaddy

Well-known member
Joined
Apr 9, 2011
Messages
222
Location
UK
Programming Experience
Beginner
hi all, i have a binding list which i want to be able to add and remove items.
This is the class im using
VB.NET:
Friend Class ExecutableItem
    Public Property Text() As String
    Public Property Method() As Action
    Public Damage() As String
End Class
then i call it
VB.NET:
Dim Car_Current As New BindingList(Of ExecutableItem)

then i bind it to a combobox
VB.NET:
Car_Select.Add(New ExecutableItem With {.Text = "Car 1", .Method = AddressOf View_car1})

but how do i remove it? i tried both of these which work but dont do exactly what i need il explain below

the delete code gets called froma sub
VB.NET:
Car_Select.RemoveAt(0)
this worked but after i delete 1 or 2 the list is no longer is order so when deleting car 4
VB.NET:
Car_Select.RemoveAt(3)
i could really be deleting car 3 as the list would be altered.

I Tried
VB.NET:
Car_Select.Remove(me.ComboBox_Car_Viewer.SelectedItem)
but since their is 3 diffrent lists that need to have items deleted at once, this is a pain for the user to make sure all 3 combobox's are on the correct item.

this is the tool tip i get
tool tip.png

i just don't understand how the items in the binding list are named so i can't name the item to remove.

i listed so much code i dont know if i have to name them when adding the item, any advice i am grateful for.
 
The items in the BindingList aren't named. They are just items sitting in a list. If you want to remove on then you either call RemoveAt and specify the index or call Remove and specify the item itself. Once you remove an item, the indexes of all items after that in the list will move to a different index. That's the way it is.

If you're saying that, for instance, you want to remove the item with a specific Text value then you have to search the list to find that item first, then remove it. As you're using .NET 4.0, you have LINQ at your disposal, which makes that relatively simple, e.g.
Dim item = myBindingList.FirstOrDefault(Function(o) o.Text = "Some text")

If item IsNot Nothing Then
    myBindingList.Remove(item)
End If
First or default will work if there are zero, one or more matches, returning Nothing for no matches or the first for one or more. If you know for a fact that there will be one and only one match then you should use Single instead of FirstOrDefault and do away with the If statement.
 
Holy Smokes... The fact that you can pass that "item" around as an Argument(as the Class that it is), and whatever Sub/Function gets it has direct access to all its Public Properties is kinda awesome... I'm beginning to understand why Global Variables should not be necessary...

I was having difficulty grabbing it out of that collection, so Thanks again, John McIlhinney!! You are awesome!

I'm glad I abandoned my DataTable approach for the BindingList(Of T)...

Although I have a hidden "_RowNumber" column in my dgv binded to my Collection/BindingList, which I always intend to sort by, it was making me nervous updating that dgv and constantly sorting it, and I'd rather update the Collection/List Members directly.
 
heyhttp://www.vbdotnetforums.com/members/jmcilhinney.html jmcilhinney, thanks for code it works perfect for all lists, i was also wondering while we are on the topic of bindinglists is their a way to set a max number of items allowed in the list while the list is stil able to delete durrent items and add items

If you just use the BindingList(Of T) class as is then you would have to explicitly test the Count property first. You could define your own class that inherits BindingList(Of T) and add that functionality by overriding the InsertItem method. The issue would be that there would be no way to notify the caller other than via an exception because the Add method has no return value. You could add a new method that returned a Boolean to indicate whether the item was successfully added or not.
 
Back
Top