I'm trying to programatically add new lines to the content of a
(System.Windows.Forms.)TextBox and, while I have found a solution, I
feel that it can't be very efficient. There must be a better way.

I first tried:

textBox.AppendText( "\n New line" );

But that didn't work. Nor did variations on the theme, like
textBox.Text += ....

Here's what *did* work:

String[] lines = new String[textBox.Lines.Length+1];
textBox.Lines.CopyTo( lines, 0 );
lines[lines.Length-1] = textBox.Text;
textBox.Lines = lines;

This seems very inefficient since we're copying the *entire* contents of
the text box just to add a single new line.

Am I missing something here?

-- T

Re: Newlines in TextBox by Andrej

Andrej
Tue May 09 02:39:34 CDT 2006

Hi Todd,

try textBox.AppendText( "\r\n New line" );

...or better yet - user Environment.NewLine instead of "\r\n\".

Andrej

"Todd Brown" <tbrown@pobox.com> wrote in message
news:5q6dnbdOTbTzkP3ZnZ2dnUVZ_umdnZ2d@comcast.com...
> I'm trying to programatically add new lines to the content of a
> (System.Windows.Forms.)TextBox and, while I have found a solution, I feel
> that it can't be very efficient. There must be a better way.
>
> I first tried:
>
> textBox.AppendText( "\n New line" );
>
> But that didn't work. Nor did variations on the theme, like textBox.Text
> += ....
>
> Here's what *did* work:
>
> String[] lines = new String[textBox.Lines.Length+1];
> textBox.Lines.CopyTo( lines, 0 );
> lines[lines.Length-1] = textBox.Text;
> textBox.Lines = lines;
>
> This seems very inefficient since we're copying the *entire* contents of
> the text box just to add a single new line.
>
> Am I missing something here?
>
> -- T



Re: Newlines in TextBox by Herfried

Herfried
Tue May 09 08:54:40 CDT 2006

"Todd Brown" <tbrown@pobox.com> schrieb:
> I'm trying to programatically add new lines to the content of a
> (System.Windows.Forms.)TextBox and, while I have found a solution, I feel
> that it can't be very efficient. There must be a better way.
>
> I first tried:
>
> textBox.AppendText( "\n New line" );
>
> But that didn't work. Nor did variations on the theme, like textBox.Text
> += ....

Windows uses "\r\n" as newline character sequence. Instead of hardcoding
"\r\n" it's maybe better to use 'Environment.NewLine', which will return a
platform-dependent newline character sequence. In addition make sure the
textbox' 'MultiLine' property is set to 'True'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>