Remove Items from a Visual Basic Collection Class

In our previous tutorials on the Visual Basic collection class we learned why would choose a Visual Basic Collection Class over an array.  We’ve also learned the basic mechanics of a collection, such as how to add items to the collection, iterate the collection, sort the collection and search the collection. In this final tutorial we will learn how to remove items from a visual basic collection class. We’ll allow our users to delete items from the class using the Remove method.

Open up the Collection class project.  Add a new button to the Form called ButtonDelete with the text Delete so it looks like so

visual-basic-collections-3

Add the following code to the ButtonDelete click event

Dim searchText As String

searchText = Trim(TextBoxISBN.Text)

If MsgBox("Are you sure you want to remove this book from the collection?", vbYesNo + vbQuestion) = vbYes Then

     collectionBooks.Remove(searchText)

     TextBoxISBN.Text = ""

End If

This fires a message box asking if the user wants to remove the book of that key from the collection.  If the user clicks yes, the item is removed.  The syntax to remove an item from the collection is

collection.Remove(key)

The key can be the key we specify or the numerical index of the item in the collection.  Remember these indexes start at 1 and end at collection.Count.

Finally this routine clears the text from the TextBoxISBN.  This in turn will fire our Text Changed Event.  This is called Event Bubbling, where an action of your code fires an event, which could in turn fire another event.  This is a source of equal pleasure and pain. Pleasure when you anticipate the bubbling.  Pain when the event was fired without you anticipating it.  Many bugs can occur because of Event Bubbling so be extra vigilant when debugging to make sure inadvertent events aren’t fired.  The debugging tools available in Visual Basic will really help you steer clear of event hell.

However we don’t want this box to be visible if the user hasn’t entered a valid ISBN.  Otherwise we’d get an “invalid key” error when we called the Remove method.

In the Form Load event, make sure the button is invisible:

ButtonDelete.Visible = False

AddItem("Microsoft Visual Basic 2010 Step By Step", "0735626693")
AddItem("Sams Teach Yourself Visual Basic 2012 in 24 Hours, Complete Starter Kit", "0672336294")
AddItem("Distributed Applications with Microsoft Visual Basic 6.0 McSd Training Kit : For Exam 70-175", "0735608334")
AddItem("Microsoft® ASP.NET Programming with Microsoft Visual Basic® .NET Version 2003 Step By Step", "0735619344")
AddItem("Visual Basic 6 Design Patterns", "0201702657")
AddItem("Excel VBA Programming For Dummies", "0470503696 ")
AddItem("Learn to Program with Visual Basic", "1902745000")
AddItem("Visual Basic 6 Complete", "0782124690")

AddBooksToListBox()

Finally change the TextBoxISBN textbox changed event to make the button visible when, and only when, an item has been found

Dim searchText As String

ListBoxBooks.Items.Clear()

searchText = Trim(TextBoxISBN.Text)

ButtonDelete.Visible = False

If searchText = "" Then

     AddBooksToListBox()

ElseIf collectionBooks.Contains(searchText) Then

     ListBoxBooks.Items.Add(collectionBooks.Item(searchText))

     ButtonDelete.Visible = True

Else

     ListBoxBooks.Items.Add("There are no books in Chris’ collection with that ISBN")

End If

You now have a fully featured app to browse and remove items from my collection.  Using the information I have given you, try extend this application to allow a user to add items to the collection.

You’re truly on your way to being a fully fledged Visual Basic developer.