BiggeRaste
Cloud Infrastructure Coder
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
200 XP
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 add record function to append a new record to the management file.
Add Record Form:
As mentioned in the previous tutorial, the first part, we created the main form design, made the writeToFile function, and I mentioned that this addRecordButton component would link to a new form where the user is able to add, remove and modify as many values for the new record to be added as they like.
Design:
So the way we are going to do this is through code instead of the designer part of the Visual Studio Ultimate 2013 application. The advantage of this is that we can keep track of how many components we have, organise them the way we like, and reference to the right ones without having to know the exact name of the ones we are looking for.
Create a new form within the same management solution project, and name it 'Add Record Form'.
-In the new 'Add Record Form'...-
So, in the designing code, we are going to have a list to hold all of the information the user wants the new/current record to hold, of course, the information will be as a string...
Lets write a small function for the addRecordButton to add a new piece of information from the textbox on the form (named 'valueText') to the information list, and update the listbox with the new information. I've named the function 'addInformation'...
We are also going to have a few components on our form;
A button named 'valueAddButton' with the text set as 'Add Information',
A textbox named 'valueText',
A button at the bottom or top named 'backButton' with the text set as 'Back',
A listbox to hold all of the current information, name this 'informationListbox',
And finally another button to delete the information from the listbox, named 'delValueButton' with the text set as 'Delete Value'.
So on the valueAddButton mouse click function we want to simply call the addInformation function, like so...
Next we want to create a simple variable to hold the value the user would like to remove when they click the delete value button...
And we also want to set the value of that new removeString string variable to the selected item whenever the item selected is changed, there is already a listbox function pre-made for this so we simply use it like so...
Now we want to program the 'deleteValueButton' button...
First we create a temporary list of string information and set it to the current list form the information list. Then we clear the global information list and begin to iterate through each piece of string information in the temporary list (which is a clone of the pre-cleared information list).
For each string within the iteration we ensure that the string is not the same as the selected string of the listbox (contained in the removeString string variable) and re-add it to the global information list of strings. If they are the same, it is the value the user wants to remove and so we do not re-add the information.
Finally we just need to make the back button which simply hides the form and shows the main form...
Showing The Form:
So that's the add user form design and coding mostly finished with, now we just need to show the form and hide the main form when the user clicks the button (opposite to the back button on the second/add user form)...
Saving The Records:
Finally now we just save the record. Add a new button, give it the text of "Save Record" and the name of "saveRecord", this will simply create a single string containing all of the information strings concatenated/compiled in to one, then send that data to the main forms writeToFile function...
There are a few things wrong in the above code because they are not correctly set yet. First we need to change the writeToFile function in the main Form1 form from Private to Public...
We also need to create a publicly avaiable filePath string name to write to the file which we will be doing in the next tutorial, lets just make it equal nothing for now (in the main Form1 form...)...
And the final explanation that I will give is that the 'info' string variable starts with 'Form1.addRecordText.Text + ", "' because it is the title that the user has entered on the main Form1 form before pressing the Add Record button 'addRecordButton'.
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 add record function to append a new record to the management file.
Add Record Form:
As mentioned in the previous tutorial, the first part, we created the main form design, made the writeToFile function, and I mentioned that this addRecordButton component would link to a new form where the user is able to add, remove and modify as many values for the new record to be added as they like.
Design:
So the way we are going to do this is through code instead of the designer part of the Visual Studio Ultimate 2013 application. The advantage of this is that we can keep track of how many components we have, organise them the way we like, and reference to the right ones without having to know the exact name of the ones we are looking for.
Create a new form within the same management solution project, and name it 'Add Record Form'.
-In the new 'Add Record Form'...-
So, in the designing code, we are going to have a list to hold all of the information the user wants the new/current record to hold, of course, the information will be as a string...
- Dim
information As
New
List(Of String
)
Lets write a small function for the addRecordButton to add a new piece of information from the textbox on the form (named 'valueText') to the information list, and update the listbox with the new information. I've named the function 'addInformation'...
- Private
Sub
addInformation()
- information.Add(valueText.Text())
- informationListbox.Items.Add(valueText.Text())
- End
Sub
We are also going to have a few components on our form;
A button named 'valueAddButton' with the text set as 'Add Information',
A textbox named 'valueText',
A button at the bottom or top named 'backButton' with the text set as 'Back',
A listbox to hold all of the current information, name this 'informationListbox',
And finally another button to delete the information from the listbox, named 'delValueButton' with the text set as 'Delete Value'.
So on the valueAddButton mouse click function we want to simply call the addInformation function, like so...
- Private
Sub
valueAddButton_Click(sender As
Object
, e As
EventArgs) Handles valueAddButton.Click
- addInformation()
- End
Sub
Next we want to create a simple variable to hold the value the user would like to remove when they click the delete value button...
- Dim
removeString As
String
And we also want to set the value of that new removeString string variable to the selected item whenever the item selected is changed, there is already a listbox function pre-made for this so we simply use it like so...
- Private
Sub
informationListbox_SelectedIndexChanged(sender As
Object
, e As
EventArgs) Handles informationListbox.SelectedIndexChanged
- removeString = informationListbox.SelectedItem
- End
Sub
Now we want to program the 'deleteValueButton' button...
- Private
Sub
delValueButton_Click(sender As
Object
, e As
EventArgs) Handles delValueButton.Click
- If
(informationListbox.SelectedIndex > -1) Then
'Is an item selected
- Dim
tempInformation As
New
List(Of String
)
- For
Each
s As
String
In
information
- tempInformation.Add(s)
- Next
- information.Clear()
- For
Each
s As
String
In
tempInformation
- Console.Write(vbNewLine + s)
- If
(s = removeString) Then
- Console.Write("Skipping "
+ s)
- Else
- information.Add(s)
- End
If
- Next
- informationListbox.Items.Clear()
- For
Each
s As
String
In
information
- informationListbox.Items.Add(s)
- Next
- End
If
- End
Sub
First we create a temporary list of string information and set it to the current list form the information list. Then we clear the global information list and begin to iterate through each piece of string information in the temporary list (which is a clone of the pre-cleared information list).
For each string within the iteration we ensure that the string is not the same as the selected string of the listbox (contained in the removeString string variable) and re-add it to the global information list of strings. If they are the same, it is the value the user wants to remove and so we do not re-add the information.
Finally we just need to make the back button which simply hides the form and shows the main form...
- Private
Sub
backButton_Click(sender As
Object
, e As
EventArgs) Handles backButton.Click
- Me.Hide()
- Form1.Show()
- End
Sub
Showing The Form:
So that's the add user form design and coding mostly finished with, now we just need to show the form and hide the main form when the user clicks the button (opposite to the back button on the second/add user form)...
- Private
Sub
addRecordButton_Click(sender As
Object
, e As
EventArgs) Handles addRecordButton.Click
- Me.Hide()
- Add_Record_Form.Show()
- End
Sub
Saving The Records:
Finally now we just save the record. Add a new button, give it the text of "Save Record" and the name of "saveRecord", this will simply create a single string containing all of the information strings concatenated/compiled in to one, then send that data to the main forms writeToFile function...
- Private
Sub
saveRecord_Click(sender As
Object
, e As
EventArgs) Handles saveRecord.Click
- Dim
info As
String
= Form1.addRecordText.Text + ", "
- For
Each
s As
String
In
information
- info += s + ", "
- Next
- Form1.writeToFile(Form1.filePath, info)
- End
Sub
There are a few things wrong in the above code because they are not correctly set yet. First we need to change the writeToFile function in the main Form1 form from Private to Public...
- Public
Sub
writeToFile...
We also need to create a publicly avaiable filePath string name to write to the file which we will be doing in the next tutorial, lets just make it equal nothing for now (in the main Form1 form...)...
- Public
filePath As
String
And the final explanation that I will give is that the 'info' string variable starts with 'Form1.addRecordText.Text + ", "' because it is the title that the user has entered on the main Form1 form before pressing the Add Record button 'addRecordButton'.