I am new to opening files and using "StreamReader", when I try to open up a file it displays all of the numbers as 0 instead of what they are, and I am not sure where my mistake is.
Also I am pretty sure I did not do saving right either..
ThanksCode:Imports System.IO Public Class ClassAverage Dim fileWriter As StreamWriter ' writes data to a text file Dim FileName As String ' name of file to save data Enum GradesFiles Grades End Enum ' places a grade in the gradesListBox Private Sub submitGradeButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles submitGradeButton.Click ' if the user entered a grade If gradeTextBox.Text <> String.Empty Then ' add the grade to the end of the gradesListBox gradesListBox.Items.Add(gradeTextBox.Text) gradeTextBox.Clear() ' clear the gradeTextBox End If gradeTextBox.Focus() ' gives the focus to the gradeTextBox End Sub ' submitGradeButton_Click ' calculates the class average based on the grades in gradesListBox Private Sub calculateAverageButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculateAverageButton.Click Dim total As Integer ' sum of grades entered by user Dim gradeCounter As Integer ' counter for grades Dim grade As Integer ' grade input by user Dim average As Double ' average of grades ' initialization phase total = 0 ' set total to zero before adding grades to it gradeCounter = 0 ' prepare to loop ' processing phase Do While gradeCounter < gradesListBox.Items.Count grade = gradesListBox.Items(gradeCounter) ' get next grade total += grade ' add grade to total gradeCounter += 1 ' add 1 to gradeCounter Loop ' termination phase If gradeCounter <> 0 Then average = total / gradesListBox.Items.Count ' calculate average ' display total and average (with two digits of precision) classAverageLabel.Text = "Total of the " & gradeCounter & " grade(s) is " & total & vbCrLf & "Class average is " & String.Format("{0:F}", average) Else classAverageLabel.Text = "No grades were entered" End If End Sub ' calculateAverageButton_Click ' clears grades from gradeListBox and results from classAverageLabel Private Sub clearGradesButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearGradesButton.Click gradesListBox.Items.Clear() ' removes all items from gradesListBox classAverageLabel.Text = String.Empty ' clears classAverageLabel End Sub ' clearGradesButton_Click Private Sub ExitToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ExitToolStripMenuItem.Click Me.Close() 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 submitGradeButton.Enabled = True gradeTextBox.Enabled = True End If DisplayGrades(GradesFiles.Grades) End Sub Sub DisplayGrades(ByVal gradesFiles As GradesFiles) Dim fileReader As StreamReader = Nothing fileReader = New StreamReader(FileName) Do While Not fileReader.EndOfStream Dim line As String = fileReader.ReadLine() Dim Grades As Integer If displayif(Grades, gradesFiles) Then gradesListBox.Items.Add(Grades & vbCrLf) End If Loop End Sub Function displayif(ByVal Grades As Integer,ByVal type As GradesFiles) As String If Grades >= 0 AndAlso type = GradesFiles.Grades Then Return True End If Return False End Function Private Sub SaveToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles SaveToolStripMenuItem.Click Dim result As DialogResult Dim filename As String 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) Catch ex As IOException MessageBox.Show("Error Opening File", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End If End Sub End Class




Reply With Quote