Creating a Management Solution in Visual Basic #4 Filtering Records #3 Modify GUI For Previous Record Altering

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 third and final part of the filter records GUI, the modify GUI. Design: First add a new Windows Form to your project by going to Project > Add New Windows Form > Windows Form > Filter Modify GUI > OK. Then give it the following design, it's very similar to the add record GUI... Textbox, textbox1, information will be entered here. Button, backButton, goes back to the filter GUI. Button, saveButton, saves the modifications. Button, delButton, deletes the selected value from the modifying record. Button, addButton, adds a new value to the record - the same as the add record GUI. Listbox, listbox1, displays current values of the currently modifying record. Showing the GUI: Now on the 'modifyButton' in the 'filterGUI' windows form we want to set some things up in the new form, show it then hide this one.
  1. Private Sub modifyButton_Click(sender As Object, e As EventArgs) Handles modifyButton.Click
  2. Dim line As String = filterList.SelectedItem
  3. Dim splits As String() = line.Split(", ")
  4. Filter_Modify_GUI.originalRecord = line
  5. Filter_Modify_GUI.values = splits.ToList()
  6. For Each s As String In splits
  7. Filter_Modify_GUI.ListBox1.Items.Add(s)
  8. Next
  9. Filter_Modify_GUI.Show()
  10. Me.Hide()
  11. End Sub
Variables: Moving on to the new 'Filter Modify GUI'. First we need to declare some variables to set from the previous 'modifyButton' click...
  1. Public originalRecord As String
  2. Public values As List(Of String)
  3. Dim removeString As String = Nothing
Delete Button: Now for the delete button. This button is almost exactly the same as the delete button found in the 'Add New Record' Form GUI but with variable references altered...
  1. Private Sub delButton_Click(sender As Object, e As EventArgs) Handles delButton.Click
  2. If (ListBox1.SelectedIndex > -1) Then 'Is an item selected
  3. Dim tempInformation As New List(Of String)
  4. For Each s As String In ListBox1.Items
  5. tempInformation.Add(s)
  6. Next
  7. ListBox1.Items.Clear()
  8. For Each s As String In tempInformation
  9. Console.Write(vbNewLine + s)
  10. If (s = removeString) Then
  11. Console.Write("Skipping " + s)
  12. Else
  13. ListBox1.Items.Add(s)
  14. End If
  15. Next
  16. End If
  17. End Sub
Add Value: The add value button this time simply adds the textbox text to the listbox...
  1. Private Sub addButton_Click(sender As Object, e As EventArgs) Handles addButton.Click
  2. ListBox1.Items.Add(TextBox1.Text)
  3. End Sub
Back Button: The back button simply hides the current form and shows the previous 'Filter GUI' Form...
  1. Private Sub backButton_Click(sender As Object, e As EventArgs) Handles backButton.Click
  2. filterGUI.Show()
  3. Me.Hide()
  4. End Sub
RemoveString Variable Value Updating: You may have noticed earlier that we created another variable for the current form GUI named 'removeString' - used just as before in the 'Add New Record' Form GUI but it does not yet have a value, other than 'Nothing'. Just as with the other form GUI, we are going to update this with the new listbox1 selected item once its selected index has changed...
  1. Private Sub listbox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
  2. removeString = ListBox1.SelectedItem
  3. End Sub
Saving: Now for the chunkiest button click function of the entire form GUI, the saving button click. This function creates a new list of strings named 'previousLines' and then runs through each line found within our management text file using a new StreamReader adding each one to the list, except the original record line, as held in our 'originalRecord' String variable, which was set on the starting the Form GUI from our previous filtering form GUI.
  1. Private Sub saveButton_Click(sender As Object, e As EventArgs) Handles saveButton.Click
  2. Dim newLine As String = Nothing
  3. Dim previousLines As List(Of String) = New List(Of String)
  4. Using sr As New System.IO.StreamReader(Form1.filePath)
  5. Dim line As String
  6. While (sr.Peek <> -1)
  7. line = sr.ReadLine()
  8. If Not (line = originalRecord) Then previousLines.Add(line)
  9. End While
  10. End Using
  11. Using sw As New System.IO.StreamWriter(Form1.filePath)
  12. For Each line As String In ListBox1.Items
  13. If Not (line = Nothing) Then newLine += line + ", "
  14. Next
  15. For Each line As String In previousLines
  16. sw.WriteLine(line)
  17. Next
  18. sw.WriteLine(newLine)
  19. End Using
  20. End Sub
We then open a new writing file stream to the file using a new StreamWriter object to the original filePath held within our Form1 GUI, and write each line to the file as required, except blank ones, followed by writing the previously found lines and the new line.

Add new comment