Question : reading text file into listbox and writing listbox items into a file at launching and exiting the application

I have two buttons which do the following:

save button: it saves the listbox items into a file;
open button: it opens the text file and displays the items into listbox;

My question is whether there is a way to open the text file at the time of launching the application and save the listbox items into the text file at the time of closing the application.

Answer : reading text file into listbox and writing listbox items into a file at launching and exiting the application

Hi mei_liu;

You need to close the StreamReader after using it to fill the list box in the form load event otherwise when you go to write to in the FormClosing event you will not be able to open it for writing.

Make sue you change the file name to match in both events

Fernando
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
    Private Sub Form1_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load
 
        Dim sr As New IO.StreamReader("c:\test.txt")
 
        While sr.Peek >= 0
 
            Dim line As String = sr.ReadLine()
            ListBox1.Items.Add(line)
 
        End While
        sr.Close()
    End Sub
Random Solutions  
 
programming4us programming4us