Remove Items from a Visual Basic Collection Class

In our previous tutorials on the Visual Basic Collection Class we’ve learned some of the basic mechanics of a Collection Class such as how to add items, iterate, sort and search the Collection. In this, the final tutorial on the Visual Basic Collection Class, we will learn how to remove items from the Collection.    With the ability to search and remove, we’re starting to develop a pretty solid maintenance app for this book Collection.

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?", CType(vbYesNo + vbQuestion, MsgBoxStyle)) = vbYes Then
       BookCollection.Remove(searchText)
       TextBoxISBN.Text = ""
End If

This code displays a message box asking if the user wants to remove the book from the Collection.  If the user clicks yes, the item is removed.   This is a new way to use a MessageBox, as a means to capture what Button the user clicks and act upon it.

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

The line above adds two buttons to the Message Box – the Yes and No buttons as well as a Question Mark icon.  This is captured in the CType(vbYesNo + vbQuestion, MsgBoxStyle).  The CType function converts vbYesNo _+ vbQuestion into a MsgBoxStyle  – the expected data type in this parameter.

If the MessageBox = vbYes i.e. the user presses Yes (you can have vbNo, vbCancel or whatever else the user may press here) then the item is removed from the Collection.

The syntax to remove an item from the Collection is:

collection.Remove(key)

We can specify which item to remove by passing the key or the numerical index of the item in the Collection.  CollectionFinally this routine clears the text from TextBoxISBN.  This obviously changes the contents of TextBoxISBN and fires the Text Changed Event of that TextBox.  This is called Event Bubbling, where an action of your code fires an event and this event could in turn fire another event.  This is a source of equal pleasure and pain when developing in Visual Basic. 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 to make sure Events aren’t fired inadvertently.  The debugging tools available in Visual Basic will really help you steer clear of event hell.

We have one final thing to consider in our application at this stage.  The user may press this Button without a valid ISBN in the search TextBox.  This will cause an error as our code as we call the Remove Method with no validation of the key at all.  One means to ensure a user doesn’t press this Button is to make the Button invisible if the user hasn’t entered a valid ISBN.

In the Form Load Event, let’s 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 TextChanged 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

That ought to do it!   You now have a fully featured app to browse and remove items from my Collection.  Use the learnings from this and previous tutorials to extend this application further and allow a user to add items to the Collection.

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