faiqaa Ответов: 1

Как сохранить и отобразить данные из текстового файла с помощью одной и той же кнопки в VB?


This form allows me to calculate the monthly average when I click the respective button, I want to be able to calculate the average display it in a label and then save this value into a text file and when calculating another average I want to be able to display the previous value calculated previously saved into the text file. The error that I keep coming across is " An unhandled exception of type 'System.ObjectDisposedException' occurred in mscorlib.dll,Additional information: Cannot read from a closed TextReader", I have tried doing some changes but I really don't know how to sort this out, has anyone got any ideas? please let me know and thanks in advance.

Что я уже пробовал:

Private Sub btnAverage_Click(sender As Object, e As EventArgs) Handles btnAverage.Click
        lblAverage2.Text = (Val(txtBoxWk1.Text) + Val(txtBoxWk2.Text) + Val(txtBoxWk3.Text) + Val(txtBoxWk4.Text)) / 4

        Dim Filenum As Integer = FreeFile()

        FileOpen(Filenum, "C:\Users\Windows 7 User\Desktop\Average.txt", OpenMode.Output)

        PrintLine(Filenum, lblSum.Text)

        FileClose(Filenum)
        MessageBox.Show("Current average has been calculated")





        Dim STUDENT_FILE As String = "C:\Users\Windows 7 User\Desktop\Average.txt"
        Dim objReader As New System.IO.StreamReader(STUDENT_FILE)

        Dim strDataline As String 'Data line
        Dim strArr(2) As String ' Array for bits of line
        Dim blfound As Boolean

        Do While objReader.Peek() <> -1 'read the file till the end.
            strDataline = (objReader.ReadLine()) ' read line and store into variable
            strArr = strDataline.Split(",") 'Split line and put into array

            label3.Text = strDataline


            blfound = True
            objReader.Close()

        Loop

        

    End Sub

1 Ответов

Рейтинг:
1

RickZeeland

Вы могли бы попробовать что-то вроде этого:

File.WriteAllText("C:\Users\Windows 7 User\Desktop\Average.txt", "blahdiblahblah")

Dim value As String = File.ReadAllText("C:\Users\Windows 7 User\Desktop\Average.txt")

Видеть: Файл.Метод WriteAllText (String, String) (System.IO)[^]