Creating a Management Solution in Visual Basic #3 Filtering Records #1 Design and Filtering Function

Introduction: This tutorial is going to be on how to create a record management solution in Visual Basic. This Tutorial: Since this is a multi-part tutorial series, this tutorial is going to be on creating the first part of the filter records GUI. What Will Filtering Do? The filtering within the program will allow the user to filter through the entire file of results and perform additional changes to any single record or a group of records based off the filtering criteria. The filtering GUI will allow the user to search by a single piece of information, a group of pieces of information, or index numbers. To create the GUI, go to Project > Add Windows Form... > select Windows Form, name it filterGUI and click OK. The Design: Before we can create any code for the filtering GUI, we need to create the design for the GUI. The GUI will contain; Textbox - named filterText - to allow the user to enter search criteria. Button - named filterButton, text set to "Fitler" - to allow the user to begin the filtering/searching process. Listbox - named filterList - to list each of the records returned from the filtering/searching process. Button - named delButton, text set to "Delete" - deletes the selected records. Button - named modifyButton, text set to "Modify" - lets the user modify that specific record. Button - named copyButton, text set to "Copy" - lets the user copy the selected record(s) to their clipboard. The Filtering Function: We won't be making any of the actual buttons within the design work in this tutorial so instead we are going to create a function which will be used once one or more of the buttons have been pressed, the filtering function. This function will take a string of information to filter the results through, and an option list of records - if there are no parsed records, we will start from fresh from the management file.
  1. Imports System.IO
  3. Public Class filterGUI
  4. Private Function filterRecords(ByVal filter As String, Optional ByVal records As List(Of String) = Nothing)
  5. Dim recs As List(Of String) = New List(Of String)
  6. If (Not records Is Nothing) Then
  7. For Each s As String In records
  8. recs.Add(s)
  9. Next
  10. Else
  11. 'Get from file
  12. Using sr As New StreamReader(Form1.filePath)
  13. While (sr.Peek <> -1)
  14. recs.Add(sr.ReadLine())
  15. End While
  16. End Using
  17. End If
  18. If (recs.Count > 0) Then
  19. Dim retRecords As List(Of String) = New List(Of String)
  20. For Each record As String In recs
  21. If (record.Contains(filter)) Then retRecords.Add(record)
  22. Next
  23. Return retRecords
  24. Else : MsgBox("No records found!")
  25. End If
  26. Return ""
  27. End Function
  28. End Class
This function gets all the available records (either from the parameter records or from the management file if no records are parsed to the records parameter), then iterates through them adding each one that contains the filtering string (from the arguments/parameters of the function, 'filter') to a new list of strings. It then returns the list of strings which all contain the filtering string. If an error occurs or no filtered records are found, a blank string is returned 'Return ""'

Add new comment