将文本附加到多行文本框时的小错误 (C#)

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

我的表单中有包含现有文本的多行文本框,我正在尝试在新行上追加文本行,一切正常,但第一行总是与现有的最后一行一起添加。

例子

文本框包含此值

测试1

我正在使用下面的代码输入新行

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

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

结果如下所示

test1line2

第 3 行

如何修复文本框的第一行以便从第二行获取新文本?

String.Join 使用定界符连接数组中的字符串。这不是你想要的。

使用:

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

注意
txtMasterResults.Text += "something"; 等同于
txtMasterResults.Text = txtMasterResults.Text + "something";

我会检索旧字符串,删除末尾的所有换行符,然后附加新内容。所以像这样:

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