Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从附加文本中删除第一行_C#_User Interface_Richtextbox - Fatal编程技术网

C# 从附加文本中删除第一行

C# 从附加文本中删除第一行,c#,user-interface,richtextbox,C#,User Interface,Richtextbox,我在VisualStudio中努力使用richtextbox。我附加了来自串行接口(uart)的通信 但是在大约1000行之后,我想删除第一行 但这应该如何运作呢 我试过: this.richTextBox_message.Text.Remove(0, 1000); // doesn't work // would be bad solution, because i want to

我在VisualStudio中努力使用richtextbox。我附加了来自串行接口(uart)的通信

但是在大约1000行之后,我想删除第一行

但这应该如何运作呢

我试过:

this.richTextBox_message.Text.Remove(0, 1000);      // doesn't work
                                                    // would be bad solution, because i want to remove lines and not chars,
this.richTextBox_message.Select(0, 100);
this.richTextBox_message.SelectedText.Remove(1);    // doesn't work

紧凑型版本

string text = this.richTextBox_message.Text;
this.richTextBox_message.Text = text.Substring(text.IndexOf('\n') + 1, text.Length - text.IndexOf('\n')-1);
解释

string text = this.richTextBox_message.Text;
this.richTextBox_message.Text = text.Substring(text.IndexOf('\n') + 1, text.Length - text.IndexOf('\n')-1);
由于
字符串
不可变的
,我们必须创建一个没有第一行的新
字符串
,并将文本框的文本设置为该行

让我们先抓取文本的副本,这样我们就不必一直写
这个.richTextBox\u message.text

string text = this.richTextBox_message.Text;
我们可以使用
Substring
方法获取字符串的一个版本,而不需要第一行。为了做到这一点,我们必须知道从哪里开始,以及我们想要抓取多少角色<代码>子字符串(整数索引,整数长度)

我们可以使用
IndexOf
查找文本中第一个出现的行分隔符字符。那正是这条线的终点。然后,我们希望添加1,以便在新文本中不包含行分隔符

int startIndex = text.Substring(text.IndexOf('\n') + 1;
现在我们需要找到我们想要得到的文本的长度。这很简单-我们需要从刚找到的startIndex到文本结尾的所有文本。我们可以从文本长度中减去startIndex,得到我们想要的长度

int length = text.Length - startIndex;
现在我们可以得到新的字符串

string newValue = text.Substring(startIndex, length);
最后将其写回text属性

this.richTextBox_message.Text = newValue;

WinForms?WPF?网络表单?MVC?我会用一个列表框;更容易删除项由于字符串是不可变的,您必须始终将修改后的值赋回
richTextBox\u message.Text
。它可以工作,但我会丢失所有文本设置(某些行是彩色的)。请尝试以下操作:
richTextBox1。选择(0,richTextBox1.GetFirstCharIndexFromLine(1));//选择第一行richTextBox1.SelectedText=“”它可以工作,但是我丢失了所有的文本设置(有些行是彩色的),您必须重新应用它们。您最好不要在这里使用
richTextBox
。使用列表框,这也会使删除第一行变得更容易。如果您确实必须使用类似文本的组件,我建议您使用AvalonEdit,为文本上色/高亮显示要容易得多,并且在删除一行后它将保留颜色。你能给代码添加一些解释,让其他人可以从中学习吗?fstam,太棒了!为其他SO用户树立榜样!