添加新文本时保留前几行

本文关键字:几行 保留 文本 添加 新文本 | 更新日期: 2024-10-18 14:29:37

我在C#Winforms richtextboxes中遇到过这样的问题:每当添加新字符串时,它都会删除以前显示的字符串并替换它。我想知道C#中是否有一个属性可以让我保留以前的字符串,并在其下添加新字符串,然后继续这样做。

添加新文本时保留前几行

这是任何语言中的基本操作,称为连接或追加文本。c#中有很多方法可以做到这一点。

 richTextBox1.Text = "Iam Line 1. ";
 //If you want to append on the same line then
 richTextBox1.Text = richTextBox1.Text + "Iam also Line 1.";
 //Or if you want to append on to the next line
 richTextBox1.Text = richTextBox1.Text + Environment.NewLine + "Iam Line 2.";
 //Also you can go to the next line simply putting 'r (Carriage Return) or 'n (New Line) Or 'r'n
 richTextBox1.Text = richTextBox1.Text + "'n" + "Iam Line 3";
 richTextBox1.Text = richTextBox1.Text + "'r" + "Iam Line 4";
 richTextBox1.Text = richTextBox1.Text + "'r'n" + "Iam Line 5";
 //You can also append using other methods like
 richTextBox1.Text += "'nIam Line 6";
 richTextBox1.Text = string.Concat(richTextBox1.Text, "'nIam Line 7");
 richTextBox1.Text = richTextBox1.Text.Insert(richTextBox1.Text.Length, "'nIam Line 8");