Alston Antony
Alston Antony

Reputation: 179

Small Bug When When Appending Text To Multiline Text Box (C#)

i have multiline text box in my form with existing text and I am trying to append text lines on new line and everything works fine but the first line always get added with the existing last line.

Example

text box holds this value

test1

and I am using below code to enter new line

txtMasterResults.AppendText(String.Join(Environment.NewLine, "line 2"));

txtMasterResults.AppendText(String.Join(Environment.NewLine, "line 3"));

and results looks like this

test1line2

line3

how can I fix the first line of textbox so I get the new text from secondline ?

Upvotes: 0

Views: 146

Answers (2)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112324

String.Join joins strings from an array using a delimiter. This is not what you want here.

Use:

txtMasterResults.Text += Environment.NewLine + "line 2" + Environment.NewLine + "line 3"; 

Note that
txtMasterResults.Text += "something"; is the same as
txtMasterResults.Text = txtMasterResults.Text + "something";

Upvotes: 1

tripflag
tripflag

Reputation: 245

I would retrieve the old string, remove any newlines at the end, and then append the new content. So like this:

txtMasterResults.Text = txtMasterResults.Text.Trim() + "\n" + newText

Upvotes: 1

Related Questions