I have to create a form that accepts three test grades for three students and then writes them to a file and displays them to a list box and then it opens the file and displays it into a different list box. I have the following code but cannot get it to work quite right. It is accepting the grades and displaying them into the first list box but I don't think it is saving them to the file created and therefore will not display the results of the file into the second list box.
Imports System.IO
Public Class GradeReport
Dim fileWriter As StreamWriter
Dim fileName As String
Dim grades(3, 2) As Integer
Dim studentCount As Integer = 1
Enum Student
Student
End Enum
Private Sub GradeReport_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
inputGradeListBox.Items.Add(vbTab & vbTab & "Test 1" & vbTab & "Test 2" & vbTab & "Test 3")
End Sub
Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
Dim result As DialogResult
Using fileChooser As New SaveFileDialog()
result = fileChooser.ShowDialog()
fileName = fileChooser.FileName
End Using
If result <> Windows.Forms.DialogResult.Cancel Then
Try
fileWriter = New StreamWriter(fileName, True)
CloseToolStripMenuItem.Enabled = True
submitGradesButton.Enabled = True
test1TextBox.Enabled = True
test2TextBox.Enabled = True
test3TextBox.Enabled = True
inputGradeListBox.Enabled = True
readGradeListBox.Enabled = True
Catch ex As IOException
MessageBox.Show("Error Opening File", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
End Sub
Private Sub submitGradesButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles submitGradesButton.Click
grades(studentCount, 0) = Convert.ToInt32(test1TextBox.Text)
grades(studentCount, 1) = Convert.ToInt32(test2TextBox.Text)
grades(studentCount, 2) = Convert.ToInt32(test2TextBox.Text)
Dim output As String = "Student" & studentCount & vbTab
For column = 0 To grades.GetUpperBound(1)
output &= vbTab & grades(studentCount, column)
Next
inputGradeListBox.Items.Add(output)
studentCount += 1
If test1TextBox.Text <> String.Empty Then
Try
Dim test1 As Integer = Convert.ToInt32(test1TextBox.Text)
If test1 > 0 Then
fileWriter.WriteLine(test1 & "," &
test2TextBox.Text & "," &
test3TextBox.Text)
Else
MessageBox.Show("Invalid Account Number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Catch ex As IOException
MessageBox.Show("Error Writing to File", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Catch ex As FormatException
MessageBox.Show("Invalid test score", "Format Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
test1TextBox.Clear()
test2TextBox.Clear()
test3TextBox.Clear()
test1TextBox.Focus()
End Sub
Private Sub CloseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseToolStripMenuItem.Click
CloseToolStripMenuItem.Enabled = False
NewToolStripMenuItem.Enabled = False
test1TextBox.Enabled = False
test2TextBox.Enabled = False
test3TextBox.Enabled = False
submitGradesButton.Enabled = False
readButton.Enabled = False
inputGradeListBox.Enabled = False
readGradeListBox.Enabled = False
OpenToolStripMenuItem.Enabled = True
End Sub
Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
Dim result As DialogResult
Using fileChooser As New OpenFileDialog()
result = fileChooser.ShowDialog()
fileName = fileChooser.FileName
End Using
If result <> Windows.Forms.DialogResult.Cancel Then
readButton.Enabled = True
End If
End Sub
Private Sub readButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles readButton.Click
Dim fileReader As StreamReader = Nothing
Try
fileReader = New StreamReader(fileName)
Do While Not fileReader.EndOfStream
Dim line As String = fileReader.ReadLine()
Dim fields() As String = line.Split(","c)
Loop
Catch ex As Exception
DisplayStudents(Student.Student)
End Try
End Sub
Sub DisplayStudents(ByVal student As Student)
Dim fileReader As StreamReader = Nothing
Try
fileReader = New StreamReader(fileName)
Catch ex As Exception
Do While Not fileReader.EndOfStream
Dim line As String = fileReader.ReadLine()
Dim fields() As String = line.Split(","c)
Dim test1 As Integer = Convert.ToInt32(fields(0))
Dim test2 As Integer = Convert.ToInt32(fields(1))
Dim test3 As Integer = Convert.ToInt32(fields(2))
Loop
Catch ex As IOException
MessageBox.Show("Cannot REad File", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
If fileReader IsNot Nothing Then
Try
fileReader.Close()
Catch ex As IOException
MessageBox.Show("Error closing file", "Error" MessageBoxButtons.OK, MessageBoxIcon.Error)
Catch ex As Exception
End Try
End If
End Try
End Sub
End Class