Saving Data

Lanc1988

Well-known member
Joined
Nov 27, 2003
Messages
508
I have some text boxes in my program that I would like them to save/remember the value of what was entered into them.

So what I need is an auto save type thing where when the form is closed, like OnClose, it will save the data to a directory.


If that is very complicated to do, I would at least like to be able to save the data on my Notepad form in the textbox. It would be fine if it were saved as a .txt file.
 
I have the following in the File then Save menu item:

Dim FileWriter As New System.IO.StreamWriter(Application.StartupPath & "\Sidekick Files\Notepad.txt")
FileWriter.WriteLine(TextBox1.Text)


But it doesnt seem to save. And if someone out there is able to get this code to work right for me, I would like to find out the code to auto load when the form is opened that file into the textbox.
 
Ok, i just tested the proble and it seems that it creates the .txt file and it can be opened from that directory, but the file is blank.. nothing at all in it.
 
The file is empty because you never close or flush the stream so the data written to the stream wont make it to the file.
Add both of those statement to your code:
Code:
FileWriter.Flush()
FileWriter.Close()
 
thanks alot it saves now, now i just need a way to get it to automatically load into "TextBox1" when the form is opened.

also, is there a way to display a dialog saying that it has saved once they click on save? if so, how?
 
To load it back into the textbox simply use System.IO.StreamReader class, it will provide with needed methods.

Right after you close the stream you could show a messagebox with the text you want using MessageBox.Show("text").
 
the message box works perfectly, but I cant find a way to write the code for the System.IO.StreamReader class..


I tried putting the following under the Form_Load:

Dim StreamReader As New System.IO.StreamReader(Application.StartupPath & "\Sidekick Files\Notepad.txt")
StreamReader.ReadLine


But it wont load, so i assume i have written the code wrong.
 
you can either add each line of your textfile one at a time like this ...
Code:
        Dim sReader As New IO.StreamReader(New IO.FileStream("C:\test.txt", IO.FileMode.Open))
       /// where your textfiles path would replace "C:\test.txt" ^^^
        While Not sReader.Peek() = -1
            TextBox1.AppendText(sReader.ReadLine & Environment.NewLine) /// add each line to your textbox one line at a time.
        End While

        sReader.Close()
or add the entire textfile in one chunk like this ...
Code:
        Dim sReader As New IO.StreamReader(New IO.FileStream("C:\test.txt", IO.FileMode.Open))

        TextBox1.AppendText(sReader.ReadToEnd)

        sReader.Close()
 

Similar threads

Back
Top