Search a Visual Basic Collection Class

In our previous tutorial on the Visual Basic Collection Class we learned how to store all my Visual Basic books in a Collection class and iterated through that list.  In addition we learned how we could sort the items in the collection.  In this next tutorial we will look at a way to search a Visual Basic Collection Class, and allow the end user of our application to search the collection for a specific book.

As mentioned in the previous tutorial, when we add an item to a Visual Basic Collection Class, we can specify a key as well.  Collection Classes are essentially made up of key value pairs.   We haven’t specified a key and instead used an items Index in the collection to find it.  In this tutorial we will key my collection of Visual Basic books by the ISBN, and allow our users to search the collection using this key.

These are my book’s ISBN.

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

Open the project “Collection Class”.

Add 2 new controls to Form1

  • A Label with the Text “Enter the ISBN”
  • A TextBox balled TextBoxISBN

Position them like so

visual-basic-collections-2

Now to the code. First, move the declaration of collectionBooks to a Form Level variable as so

Private collectionBooks As New Collection

Remove the Collection parameter from our AddBooksToListBox routine as so

Private Sub AddBooksToListBox()

     For i = 1 To collectionBooks.Count
          ListBoxBooks.Items.Add(collectionBooks.Item(i).ToString)
     Next

End Sub

Next, replace the AddItem subroutine with this

' Add an item at its correct position.
Private Sub AddItem(ByVal item As String, ByVal key As String)

     Dim i As Integer

     ' See where the item belongs. Iterate through the entire list
     For i = 1 To collectionBooks.Count

          If collectionBooks.Item(i) >= item Then
               Exit For
          End If

     Next i

     ' Insert the item.
     If i > collectionBooks.Count Then

          ' Add at the end.
          collectionBooks.Add(item, key)

     Else

          ' Add at the right position.
          collectionBooks.Add(item, key, i)

     End If

End Sub

As well as removing the collectionBooks parameter, we’ve added a new String parameter called “key” which we will use to add the key of the collection item.

The body of the code has been slightly altered too

' Insert the item.
 If i > collectionBooks.Count Then

     ' Add at the end.
     collectionBooks.Add(item, key)

Else

     ' Add at the right position.
     collectionBooks.Add(item, key, i)

End If

Specifying the key as part of the call to the Add method

Replace the Form Load with

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

AddBooksToListBox()

Removing the colectionBooks declaration while passing the ISBN to our new AddItem routine.

In the TextBoxISBN Text Changed Event add the following code

Private Sub TextBoxISBN_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBoxISBN.TextChanged

     Dim searchText As String

     ListBoxBooks.Items.Clear()

     searchText = Trim(TextBoxISBN.Text)

     If searchText = "" Then

          AddBooksToListBox()

     ElseIf collectionBooks.Contains(searchText) Then

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

     Else

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

     End If

End Sub

Every time the contents of the TextBox changes, e.g. when the user types, this event will fire. Much like Google nowadays.  Whenever the user types, the system will search the collection for a book with that ISBN.

If we look at the body of code:

  • The first line clears all items from our ListBox.
  • The variable searchText is populated with the contents of the Text in TextBoxISBN.  The Trim function removes all newlines, spaces  and tabs from the beginning and end of the supplied string.  This is an extremely useful function.  The user can’t see e.g. spaces so may leave them in the TextBox accidentally.  This function anticipates the user doing this and removes any characters from the string that the user can’t see.  It improves useability no end.
  • The first IF Clause checks to see if the variable searchText is empty.  If so then all books are added to the ListBox.
  • The second IF Clause uses the Contains function to see if the collection contains that ISBN key.   If it does then that book is displayed.
  • The final Else clause runs only when there are characters in the TextBox but the collection did not Contain any items with that ISBN.  The user is informed that “There are no books in Chris’ collection with that ISBN”

Run the project and see the results.  You can search the collection for the ISBN key we added to the collection.

So far we have learned why we would use collections instead of e.g. arrays.  Now we’ve learned how to sort and search our collection.

Next, in our final tutorial on Visual Basic collection, we will be removing items from our collection.