Search for items in a Visual Basic Collection Class

In the previous tutorials 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, allowing users of our application to search for a specific book in the Collection.

Collection Classes are essentially made up of key value pairs.   We can retrieve values from the Collection using the key, and we can specify the key when we add items to the Collection.  So far, we haven’t specified any key and instead we’ve used the items numerical 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.

The following table details these ISBN numbers:

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

To add these to our Collection, open the Project “Collection Class”.

Add 2 new controls to Form1:

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

Position them like so:

visual-basic-collections-2

Double click on the form to get to the code. Move the declaration of CollectionBooks to a Form Level Variable as so:

Private collectionBooks As New Collection

This makes the Variable accessible anywhere in the Form.  We don’t need to pass this Variable as a parameter to routines in the Form now, so 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 the following code:

' 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.  The key is specified when calling the Collection.Add method as shown below:

' 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

Replace the Form Load with the following code, removing the local collectionBooks declaration while passing the ISBN key to our new AddItem routine.

</span>
<pre>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()

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 in the TextBox, this event will fire. This search routine works much like Google search nowadays, searching every time you type a new letter in the search box.  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 empty spaces 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 type 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 and 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.