Newline in Textbox

K

Kaka

Hi, I am a learner for C# and .Net.

I want to know how to put a line break to a string so that when it show in
the textbox the text will be displayed in saperated lines.

e.g.:

String s;
s = "AAAAAAAAAAAAAAAAAAAAA";
s += "BBBBBBBBBBBBBBBBBBBBBBB";
txtOutput.Text = s;

I want to show them like:
AAAAAAAAAAAAAAAAAAAAA
BBBBBBBBBBBBBBBBBBBBBBB

But the output is always in one line:
AAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBBBB

Even I include a "\n" at the end at the first line:
s = "AAAAAAAAAAAAAAAAAAAAA\n";

Thanks
 
C

Chison

hi,kaka

First make sure the attribute "TextMode" of the textbox have set

to "MultiLine" . Then u can out put the text displayed in saperated lines by

this 2 ways:

1. String s;
s = @"AAAAAAAAAAAAAAAAAAAAA
BBBBBBBBBBBBBBBBBBBBBBB";
txtOutput.Text = s;

2. String s;
s = "AAAAAAAAAAAAAAAAAAAAA";
s += "\nBBBBBBBBBBBBBBBBBBBBBBB";
txtOutput.Text = s;


Hope that is what you were looking for.
 
C

C# Learner

Chison said:
1. String s;
s = @"AAAAAAAAAAAAAAAAAAAAA
BBBBBBBBBBBBBBBBBBBBBBB";
txtOutput.Text = s;

This'll give you a newline and several spaces.
2. String s;
s = "AAAAAAAAAAAAAAAAAAAAA";
s += "\nBBBBBBBBBBBBBBBBBBBBBBB";
txtOutput.Text = s;

This won't give you a newline. Use "\r\n".
 
C

C# Learner

Gerald Baeck said:
s = "AAAAAAAAAAAAAAAAAAAAA" + Environment.NewLine;
s += "BBBBBBBBBBBBBBBBBBBBBBB";

Here're a couple of notes about using "\r\n" instead:
<http://tinyurl.com/4pxg9>.

Also note that such string concatenation is inefficient. Using something
like the following would've been better:

s = "AAAAAAAAAAAAAAAAAAAAA" + Environment.NewLine +
"BBBBBBBBBBBBBBBBBBBBBBB";

This effects a single call to String.Concat, avoiding the unnecessary
creation of a temporary string object.
 
J

Jon Skeet [C# MVP]

C# Learner said:
Here're a couple of notes about using "\r\n" instead:
<http://tinyurl.com/4pxg9>.

Also note that such string concatenation is inefficient. Using something
like the following would've been better:

s = "AAAAAAAAAAAAAAAAAAAAA" + Environment.NewLine +
"BBBBBBBBBBBBBBBBBBBBBBB";

This effects a single call to String.Concat, avoiding the unnecessary
creation of a temporary string object.

Alternatively, use

s = "AAAAAAAAAAAAAAAAAAAAA"+"\r\n"+
"BBBBBBBBBBBBBBBBBBBBBBB";

so that the compiler does all the concatenation, rather than the
runtime.

I'm slightly wary of using Environment.NewLine for text boxes - as I
understand it, it's really what the operating system sees as the
default newline string for files, which may not be what text boxes need
to use. (For instance, I'm not sure what a Mono port of TextBox would
use - it might use \r\n for compatibility with code developed on
Windows, but then that would break if you used Environment.NewLine...
 

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