Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

Posted

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.

  • *Experts*
Posted

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()

Posted

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?

  • *Experts*
Posted

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").

Posted

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.

  • Leaders
Posted

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()

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...