TextBox Query

P

Patrick De Ridder

Sorry, this must be very elementary:

When writing line 1 to a textBox,
pressing return
writing line 2 to same tetBox.

Saving that to disc.
using (StreamWriter sw = new StreamWriter ("memo.txt"))
{
sw.WriteLine(textBox1.Text);
}

Reading that from disk.
using (StreamReader sr = new StreamReader ("memo.txt"))
{
textBox1.Text=sr.ReadLine();
}

I only get the line 1 back.
Q: How do I get all lines back?
 
P

Pete Davis

Well, first of all, you probably don't want to use ReadLine and WriteLine
since you're only writing one line (the first or last one, I suspect) and
reading that same one line. Instead StreamWriter.Write() the entire
textBox.Text buffer. Then just StreamReader.Read() the entire buffer back
and you'll get your linefeeds and carriage returns.

Pete
 
V

Vijaye Raji

You'll have to loop it until there's no more to be read.

like,

string newLine;
while ((newLine = sr.ReadLine) != null)
{
textBox1.Append(newLine);
}

-vJ
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top