• We just launched and are currently in beta. Join us as we build and grow the community.

Creating a Management Solution in Visual Basic #2 Add Record

BiggeRaste

Cloud Infrastructure Coder
B Rep
0
0
0
Rep
0
B Vouches
0
0
0
Vouches
0
Posts
82
Likes
122
Bits
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...

  1. 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'...

  1. Private

    Sub

    addInformation()
  2. information.Add(valueText.Text())
  3. informationListbox.Items.Add(valueText.Text())
  4. 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...

  1. Private

    Sub

    valueAddButton_Click(sender As

    Object

    , e As

    EventArgs) Handles valueAddButton.Click
  2. addInformation()
  3. 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...

  1. 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...

  1. Private

    Sub

    informationListbox_SelectedIndexChanged(sender As

    Object

    , e As

    EventArgs) Handles informationListbox.SelectedIndexChanged
  2. removeString = informationListbox.SelectedItem
  3. End

    Sub

Now we want to program the 'deleteValueButton' button...

  1. Private

    Sub

    delValueButton_Click(sender As

    Object

    , e As

    EventArgs) Handles delValueButton.Click
  2. If

    (informationListbox.SelectedIndex > -1) Then

    'Is an item selected
  3. Dim

    tempInformation As

    New

    List(Of String

    )
  4. For

    Each

    s As

    String

    In

    information
  5. tempInformation.Add(s)
  6. Next
  7. information.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. information.Add(s)
  14. End

    If
  15. Next
  16. informationListbox.Items.Clear()
  17. For

    Each

    s As

    String

    In

    information
  18. informationListbox.Items.Add(s)
  19. Next
  20. End

    If
  21. 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...

  1. Private

    Sub

    backButton_Click(sender As

    Object

    , e As

    EventArgs) Handles backButton.Click
  2. Me.Hide()
  3. Form1.Show()
  4. 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)...

  1. Private

    Sub

    addRecordButton_Click(sender As

    Object

    , e As

    EventArgs) Handles addRecordButton.Click
  2. Me.Hide()
  3. Add_Record_Form.Show()
  4. 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...

  1. Private

    Sub

    saveRecord_Click(sender As

    Object

    , e As

    EventArgs) Handles saveRecord.Click
  2. Dim

    info As

    String

    = Form1.addRecordText.Text + ", "
  3. For

    Each

    s As

    String

    In

    information
  4. info += s + ", "
  5. Next
  6. Form1.writeToFile(Form1.filePath, info)
  7. 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...

  1. 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...)...

  1. 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'.

 

452,292

324,736

324,744

Top