Creating a Management Solution in Visual Basic #4 Filtering Records #3 Modify GUI For Previous Record Altering
Submitted by Yorkiebar on Thursday, April 10, 2014 - 10:34.
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.
Variables:
Moving on to the new 'Filter Modify GUI'. First we need to declare some variables to set from the previous 'modifyButton' click...
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...
Add Value:
The add value button this time simply adds the textbox text to the listbox...
Back Button:
The back button simply hides the current form and shows the previous 'Filter GUI' Form...
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...
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.
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.
- Private Sub modifyButton_Click(sender As Object, e As EventArgs) Handles modifyButton.Click
- Dim line As String = filterList.SelectedItem
- Dim splits As String() = line.Split(", ")
- Filter_Modify_GUI.originalRecord = line
- Filter_Modify_GUI.values = splits.ToList()
- For Each s As String In splits
- Filter_Modify_GUI.ListBox1.Items.Add(s)
- Next
- Filter_Modify_GUI.Show()
- Me.Hide()
- End Sub
- Public originalRecord As String
- Public values As List(Of String)
- Dim removeString As String = Nothing
- Private Sub delButton_Click(sender As Object, e As EventArgs) Handles delButton.Click
- If (ListBox1.SelectedIndex > -1) Then 'Is an item selected
- Dim tempInformation As New List(Of String)
- For Each s As String In ListBox1.Items
- tempInformation.Add(s)
- Next
- ListBox1.Items.Clear()
- For Each s As String In tempInformation
- Console.Write(vbNewLine + s)
- If (s = removeString) Then
- Console.Write("Skipping " + s)
- Else
- ListBox1.Items.Add(s)
- End If
- Next
- End If
- End Sub
- Private Sub addButton_Click(sender As Object, e As EventArgs) Handles addButton.Click
- ListBox1.Items.Add(TextBox1.Text)
- End Sub
- Private Sub backButton_Click(sender As Object, e As EventArgs) Handles backButton.Click
- filterGUI.Show()
- Me.Hide()
- End Sub
- Private Sub listbox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
- removeString = ListBox1.SelectedItem
- End Sub
- Private Sub saveButton_Click(sender As Object, e As EventArgs) Handles saveButton.Click
- Dim newLine As String = Nothing
- Dim previousLines As List(Of String) = New List(Of String)
- Using sr As New System.IO.StreamReader(Form1.filePath)
- Dim line As String
- While (sr.Peek <> -1)
- line = sr.ReadLine()
- If Not (line = originalRecord) Then previousLines.Add(line)
- End While
- End Using
- Using sw As New System.IO.StreamWriter(Form1.filePath)
- For Each line As String In ListBox1.Items
- If Not (line = Nothing) Then newLine += line + ", "
- Next
- For Each line As String In previousLines
- sw.WriteLine(line)
- Next
- sw.WriteLine(newLine)
- End Using
- End Sub
Add new comment
- 7 views