Creating a Management Solution in Visual Basic #3 Filtering Records #1 Design and Filtering Function
Submitted by Yorkiebar on Tuesday, April 8, 2014 - 08:11.
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.
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 ""'
- Imports System.IO
- Public Class filterGUI
- Private Function filterRecords(ByVal filter As String, Optional ByVal records As List(Of String) = Nothing)
- Dim recs As List(Of String) = New List(Of String)
- If (Not records Is Nothing) Then
- For Each s As String In records
- recs.Add(s)
- Next
- Else
- 'Get from file
- Using sr As New StreamReader(Form1.filePath)
- While (sr.Peek <> -1)
- recs.Add(sr.ReadLine())
- End While
- End Using
- End If
- If (recs.Count > 0) Then
- Dim retRecords As List(Of String) = New List(Of String)
- For Each record As String In recs
- If (record.Contains(filter)) Then retRecords.Add(record)
- Next
- Return retRecords
- Else : MsgBox("No records found!")
- End If
- Return ""
- End Function
- End Class
Add new comment
- 3 views