Carlos
Carlos

Reputation: 125

New lines dissapear when reading .txt from Android

Doing this

String t = "asd\nasd";

TextBox objTxt = (TextBox)messageBox;
bjTxt.Text = t;

doesnt show

asd    
asd

as expected, it shows

asdasd

why, its driving me crazy. TextBox is set to multiline and I can write lines in it manually. Thanks!

Upvotes: 0

Views: 92

Answers (2)

Nikola Davidovic
Nikola Davidovic

Reputation: 8656

TextBox unlike Label and MessageBox ignores "\n" so if you want to get to the newline you will need to use "\r\n" combination. Nevertheless there is a better solution, just use Environment.NewLine and you won't need to think about \r\n combination for a newline.

Either:

String t = "asd\r\nasd";

Or:

String t = "asd" + Environment.NewLine + "asd";

The beautiful thing about Environment.NewLine is that there is no need to worry about the newline in any environment for which you are developing (or at least it should be that way).

EDIT:

I saw your comment, so I'll add few words. You could still use ReadToEnd() and if the text contains only "\n" for newline, you could do the following:

t = t.Replace("\n", "\r\n");

Or:

t = t.Replace("\n", Environment.NewLine);

since Environment.NewLine is essentially a string

Upvotes: 2

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236268

Try to use Lines property of TextBox to assign all lines from file:

textBox.Lines = File.ReadAllLines(fileName);

And, as I stated in comment above, for your sample you should use Environment.NewLine for new line to appear in TextBox:

textBox.Text = "asd" + Environment.NewLine + "asd";

Upvotes: 0

Related Questions