CODE
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Dim sr As System.IO.StreamWriter
Dim k As Integer
If MessageBox.Show("Want to quit?", "Quit?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
e.Cancel = True
Else
sr = IO.File.AppendText("info.txt")
For k = 0 To gIndex
sr.WriteLine(gStudentArray(k).Name & vbTab & gStudentArray(k).Department & vbTab & gStudentArray(k).Score.ToString & vbTab & gStudentArray(k).Gender & vbTab & gStudentArray(k).Deans)
Next
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sr As System.IO.StreamReader
Dim Line As String
If System.IO.File.Exists("info.txt") = False Then
MessageBox.Show(" There is no input file.")
Exit Sub
End If
sr = System.IO.File.OpenText("info.txt")
Do Until sr.Peek = -1
Line = sr.ReadLine()
lstStudents.Items.Add(Line)
gIndex = gIndex + 1
ReDim Preserve gStudentArray(gIndex)
Loop
sr.Close()
End Sub
Alright so what im trying to do is have the program read infromation from a text file called info.txt or create it if its not already there. If there is information in the file I want it to be displayed in the listbox lstStudents.
When the file is closed, I want it to ask the user if they want to quit and then if they say yes have all the information saved to the Info.txt file.
I feel like for the most part my code is right but Im still having trouble totally grasping the concept of writting and retrieving info.