Lanc1988 Posted February 18, 2004 Posted February 18, 2004 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. Quote
Lanc1988 Posted February 18, 2004 Author Posted February 18, 2004 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 doesn't 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. Quote
Lanc1988 Posted February 18, 2004 Author Posted February 18, 2004 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. Quote
*Experts* mutant Posted February 18, 2004 *Experts* Posted February 18, 2004 The file is empty because you never close or flush the stream so the data written to the stream won't make it to the file. Add both of those statement to your code: FileWriter.Flush() FileWriter.Close() Quote
Lanc1988 Posted February 18, 2004 Author Posted February 18, 2004 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? Quote
*Experts* mutant Posted February 18, 2004 *Experts* Posted February 18, 2004 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"). Quote
Lanc1988 Posted February 19, 2004 Author Posted February 19, 2004 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. Quote
Lanc1988 Posted February 19, 2004 Author Posted February 19, 2004 anyone? how can i get it to load the .txt file when the form is opened? Quote
Leaders dynamic_sysop Posted February 19, 2004 Leaders Posted February 19, 2004 you can either add each line of your textfile one at a time like this ... Dim sReader As New IO.StreamReader(New IO.FileStream("C:\test.txt", IO.FileMode.Open)) '/// where your textfile's 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 ... Dim sReader As New IO.StreamReader(New IO.FileStream("C:\test.txt", IO.FileMode.Open)) TextBox1.AppendText(sReader.ReadToEnd) sReader.Close() Quote
Lanc1988 Posted February 19, 2004 Author Posted February 19, 2004 thanks so much, it works perfect Quote
Leaders dynamic_sysop Posted February 19, 2004 Leaders Posted February 19, 2004 your welcome :) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.