title: Creating an array of student names and scores in Visual Basic that updates a list box with the information
body: <p>I am working on a project that has multiple forms. The main form has a listBox that displays the contents of a tab delimited text file. The text file looks like this:</p>
  
  <p><code>
  Bowser, William C   84  98  77  83  75  92
  Smithson, Anne K    76  87  65
  Brewer, Juliette    23
  Oblonski, Walter H  88  97  86  75  93  98
  Smith, John D   86  75  84  93  91  78 
  Katinga, Raphael P  56  87  44  32  22
  Robertson, Gerald T 67  78  87  93  87  75
  Smith, John B   89  67  75  84  92  83</code></p>
  
  <p>The assignment is as follows:</p>
  
  <blockquote>
    <p>StudentScores</p>
    
    <p>Develop an application that reads in student names (limited to 20
    students) and scores (up to 6 per student) from a tab delimited file,
    StudentScores.txt (in Student Files). The application shall allow the
    user to add students to the student set, add and change scores for a
    student in the student set, delete a student and the student’s scores
    from the student set, and save the student set back to the
    StudentScores.txt file.</p>
  </blockquote>
  
  <p>I am not having any luck getting it to correctly add a new student &amp; scores or save the current "updated" information. I think I am not using my arrays correctly.
  below is my code for the main form (frmStudentScores) and a second form (frmAddNewStudent) </p>
  
  <p>My frmStudentScores code:</p>
  
  <pre><code>Imports System.IO
  Imports System.Text
  
  Public Class frmStudentScores
  
      ' frmStudentScores top level declarations
      Dim ScoreTotal As Integer = 0
      Dim ScoreCount As Integer = 0
      Dim ScoreAverage As Integer = 0
  
      ' Executing the program shall cause reading of the StudentScores.txt file and display.
      Private Sub frmStudentScores_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  
          ' Set the title bar to date and time
          Me.Text = "StudentScores Scores - " &amp; DateTime.Now 'String.Format("{0:d}", currentDate)
  
          ' Read the file into parallel string arrays 
          Dim AllLines() As String = File.ReadAllLines(filePath_Read)
          Dim Columns(20) As String
  
          ' Parsing file lines into columns
          For LineIndex As Int32 = 0 To intFileLineCount
              Columns = AllLines(LineIndex).Split(vbTab)
              S_NAME(LineIndex) = Columns(0)
              S_SCORE1(LineIndex) = Columns(1)
              S_SCORE2(LineIndex) = Columns(2)
              S_SCORE3(LineIndex) = Columns(3)
              S_SCORE4(LineIndex) = Columns(4)
              S_SCORE5(LineIndex) = Columns(5)
              S_SCORE6(LineIndex) = Columns(6)
  
          Next
  
          ' Display the data in a ListBox;
          Dim Row As String = 0
  
          For LineIndex As Int32 = 0 To intFileLineCount
  
              Row = String.Format("{0,-30}{1,5}{2,5}{3,5}{4,5}{5,5}{6,5}", _
                              S_NAME(LineIndex), _
                              S_SCORE1(LineIndex), _
                              S_SCORE2(LineIndex), _
                              S_SCORE3(LineIndex), _
                              S_SCORE4(LineIndex), _
                              S_SCORE5(LineIndex), _
                              S_SCORE6(LineIndex))
  
              lstBxOutput.Items.Add(Row)
  
          Next
  
          ' Calculates the total score, how many scores and average score of the selected student.
          If lstBxOutput.SelectedIndex &lt;&gt; -1 Then
              ' Make the currently selected item the top item in the ListBox.
  
              For Index As Int32 = 1 To 6
  
                  If Columns(Index) &lt;&gt; " " And Columns(Index) &lt;&gt; Nothing Then
  
                      ScoreTotal = ScoreTotal + Val(Columns(Index))
                      ScoreCount = ScoreCount + 1
                      ScoreAverage = ScoreTotal / ScoreCount
  
                  End If
  
              Next
          End If
  
          txtBxScoreTotal.Text = ScoreTotal
          txtBxScoreCount.Text = ScoreCount
          txtBxAverage.Text = ScoreAverage
  
      End Sub
  
  '**********************************    On List Box Selection Change   **********************************************
  '@===============================================================================@'
  '@  To display the total, count, and average for a student, the user selects the @'
  '@  student from the list box. If the list box is empty, the total, count, and   @'
  '@  average labels should be cleared                                             @'
  '@===============================================================================@'
  Private Sub lstBxOutput_SelectedIndexChanged(sender As Object, e As EventArgs)Handles lstBxOutput.SelectedIndexChanged
  
      'Change Score Total, Score Count and Average to reflect the new data selected
      Dim Colum(7) As String
  
      'Colum(0) = S_NAME(lstBxOutput.SelectedIndex)
      Colum(1) = S_SCORE1(lstBxOutput.SelectedIndex)
      Colum(2) = S_SCORE2(lstBxOutput.SelectedIndex)
      Colum(3) = S_SCORE3(lstBxOutput.SelectedIndex)
      Colum(4) = S_SCORE4(lstBxOutput.SelectedIndex)
      Colum(5) = S_SCORE5(lstBxOutput.SelectedIndex)
      Colum(6) = S_SCORE6(lstBxOutput.SelectedIndex)
  
      'For loop checks to see if is a value in Colum(Index) and if there is, it calculates 
      'the Total Score, How many scores have been entered and the average of the entered scores.
      For Index As Int32 = 1 To 6
  
          If Colum(Index) &lt;&gt; " " And Colum(Index) &lt;&gt; Nothing Then
  
              ScoreTotal = ScoreTotal + Val(Colum(Index))
              ScoreCount = ScoreCount + 1
              ScoreAverage = ScoreTotal / ScoreCount
  
          End If
  
      Next
      'Display the Total Score, How many scores have been entered and the average of 
      'the entered scores in the appropriate text boxes
      txtBxScoreTotal.Text = ScoreTotal
      txtBxScoreCount.Text = ScoreCount
      txtBxAverage.Text = ScoreAverage
      ' Reset the ScoreTotal, ScoreCount and ScoreAverage to zero
      ScoreAverage = 0
      ScoreTotal = 0
      ScoreCount = 0
      ScoreAverage = 0
  
  End Sub
  
  Private Sub btnAddNew_Click(sender As Object, e As EventArgs) Handles btnAddNew.Click
  
      'To add a new student, the user clicks the Add button to display the Add New Student dialog box.
      frmAddNewStudent.Show()
      Me.Hide()
  
  End Sub
  
  Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
  
      'To update an existing student’s scores, the user selects the student in the list box and 
      'clicks the Update button to display the Update Student Scores dialog box.
      frmUpdateScore.Show()
      Me.Hide()
  
  End Sub
  
  Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
      'To delete a student, the user selects the student in the list box and clicks the Delete button.
  End Sub
  
  Private Sub btnReport_Click(sender As Object, e As EventArgs) Handles btnReport.Click
      'To display a formatted report listing the student names, their scores, their averages, and 
      'their grade, the user selects the Report button. Report design is left to the 
      'COP 1332 student, however, it shall be columnar formatted.
  End Sub
  
  Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
  
      'The Save button may be pressed at any time. This shall cause overwriting of the 
      'StudentScores.txt file contents with the current contents of the student set from 
      'that stored in the program.
  
      ' Save to filePath_Write
      Dim myStreamWriter As New StreamWriter(filePath_Write)
  
      ' Prepare File content...
      For LineIndex As Int32 = 0 To lstBxOutput.Items.Count '- 1
          myStreamWriter.WriteLine(String.Format("{1}{0}{2}{0}{3}{0}{4}{0}{5}{0}{6}{0}{7}", _
                vbTab, _
                S_NAME(LineIndex), _
                S_SCORE1(LineIndex), _
                S_SCORE2(LineIndex), _
                S_SCORE3(LineIndex), _
                S_SCORE4(LineIndex), _
                S_SCORE5(LineIndex), _
                S_SCORE6(LineIndex)))
      Next
  
      myStreamWriter.WriteLine(String.Format("{1}{0}{2}{0}{3}{0}{4}{0}{5}{0}{6}{0}{7}", _
                vbTab, _
                S_NAME(intFileLineCount), _
                S_SCORE1(intFileLineCount), _
                S_SCORE2(intFileLineCount), _
                S_SCORE3(intFileLineCount), _
                S_SCORE4(intFileLineCount), _
                S_SCORE5(intFileLineCount), _
                S_SCORE6(intFileLineCount)))
  
      'For i = 0 To lstBxOutput.Items.Count - 1
      '    myStreamWriter.WriteLine(lstBxOutput.Items.Item(i))
      'Next
  
      myStreamWriter.Close()
  
  End Sub
  
  Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
  
      'exit the program
      Me.Close()
  
  End Sub
  </code></pre>
  
  <p>The code for form frmAddNewStudent (used to add a new student) is:</p>
  
  <pre><code>Public Class frmAddNewStudent
  
      ' frmAddNewStudent top level vars
      Dim int_Counter As Integer = 0
  
      ' adds a user entered value to the txtBxScores.Text string
      Private Sub btnAddScore_Click(sender As Object, e As EventArgs) Handles btnAddScore.Click
  
          ' adds the value to array SelectedScoreVals()
          SelectedScoreVals(int_Counter) = txtBxScore.Text
  
          txtBxScores.Text = txtBxScores.Text + " " + txtBxScore.Text
          txtBxScore.Text = Nothing
          txtBxScore.Focus()
  
          int_Counter += 1
  
      End Sub
  
      ' adds a row to frmStudentScores.lstBxOutput
      Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
  
          If txtBxName.Text = Nothing Then
              MessageBox.Show("Please enter a Name")
              txtBxName.Focus()
              Exit Sub
          End If
  
  
          S_NAME(intFileLineCount + 1) = txtBxName.Text
          S_SCORE1(intFileLineCount + 1) = SelectedScoreVals(0)
          S_SCORE2(intFileLineCount + 1) = SelectedScoreVals(1)
          S_SCORE3(intFileLineCount + 1) = SelectedScoreVals(2)
          S_SCORE4(intFileLineCount + 1) = SelectedScoreVals(3)
          S_SCORE5(intFileLineCount + 1) = SelectedScoreVals(4)
          S_SCORE6(intFileLineCount + 1) = SelectedScoreVals(5)
  
          MessageBox.Show("The listBox Index is: " + frmStudentScores.lstBxOutput.SelectedIndex.ToString + "    " + _
                                 "The scores for the selected Index are:  " + _
                                 SelectedScoreVals(0).ToString + "  " + _
                                 SelectedScoreVals(0).ToString + "  " + _
                                 SelectedScoreVals(0).ToString + "  " + _
                                 SelectedScoreVals(0).ToString + "  " + _
                                 SelectedScoreVals(0).ToString + "  " + _
                                 SelectedScoreVals(0).ToString)
  
  
          Dim Row As String = Nothing
  
          Row = String.Format("{0,-30}{1,5}{2,5}{3,5}{4,5}{5,5}{6,5}", _
                                S_NAME(intFileLineCount), _
                                S_SCORE1(intFileLineCount), _
                                S_SCORE2(intFileLineCount), _
                                S_SCORE3(intFileLineCount), _
                                S_SCORE4(intFileLineCount), _
                                S_SCORE5(intFileLineCount), _
                                S_SCORE6(intFileLineCount))
  
  
          frmStudentScores.lstBxOutput.Items.Add(Row)
  
          ' Clean up and close
          Array.Clear(SelectedScoreVals, 0, 6)
          frmStudentScores.Show()
          Me.Close()
  
      End Sub
  
      Private Sub btnClearScores_Click(sender As Object, e As EventArgs) Handles btnClearScores.Click
  
          txtBxScores.Text = ""
          int_Counter = 0
  
      End Sub
  
      ' Returns you to frmStudentScores
      Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
  
          frmStudentScores.Show()
          Me.Close()
  
      End Sub
  </code></pre>
  
  <p>Also I have a Module with the name and scores arrays in it:</p>
  
  <pre><code>Imports System.IO
  Public Module mdlGlobals
  
      ' Sets the directory paths to variables
      Dim dirPath As String = "C:\Users\dehnb\Documents\Visual Studio 2012\Projects\VBFinal\StudentScores\"
      Public filePath_Read As String = dirPath &amp; "StudentScores.txt"
      Public filePath_Write As String = dirPath &amp; "StudentScoresWright.txt"
  
      ' Declare parallel string arrays 
      Public S_NAME(20) As String
      Public S_SCORE1(20) As String
      Public S_SCORE2(20) As String
      Public S_SCORE3(20) As String
      Public S_SCORE4(20) As String
      Public S_SCORE5(20) As String
      Public S_SCORE6(20) As String
  
      'Public aryStudentData()() As String = New String(6)() { _
      '                      S_NAME, _
      '                      S_SCORE1, _
      '                      S_SCORE2, _
      '                      S_SCORE3, _
      '                      S_SCORE4, _
      '                      S_SCORE5, _
      '                      S_SCORE6}
  
  
      Dim AllLines() As String = File.ReadAllLines(filePath_Read)
  
      Public SelectedScoreVals(6) As String
      Public intFileLineCount As Integer = File.ReadAllLines(filePath_Read).Length - 1
  
  End Module
  </code></pre>