删除.NET RichTextBox中的特定行
如何删除RichTextBox中的特定文本行?
另一种方案:
private void DeleteLine(int a_line) { int start_index = richTextBox.GetFirstCharIndexFromLine(a_line); int count = richTextBox.Lines[a_line].Length; // Eat new line chars if (a_line < richTextBox.Lines.Length - 1) { count += richTextBox.GetFirstCharIndexFromLine(a_line + 1) - ((start_index + count - 1) + 1); } richTextBox.Text = richTextBox.Text.Remove(start_index, count); }
基于tomanu的解决方案,但没有开销
int start_index = LogBox.GetFirstCharIndexFromLine(linescount); int count = LogBox.GetFirstCharIndexFromLine(linescount + 1) - start_index; LogBox.Text = LogBox.Text.Remove(start_index, count);
请注意,我的linecount在这里是linescount – 2。
这也可以解决问题(如果你可以在表单代码中处理诸如++之类的东西)。 保留文本格式。 只需记住“ReadOnly”属性对您和用户都有效。
richTextBox.SelectionStart = richTextBox.GetFirstCharIndexFromLine(your_line); richTextBox.SelectionLength = this.richTextBox.Lines[your_line].Length+1; this.richTextBox.SelectedText = String.Empty;
试试这个:
Dim lst As New ListBox Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Me.Controls.Add(lst) For Each cosa As String In Me.RichTextBox1.Lines lst.Items.Add(cosa) Next lst.Items.RemoveAt(2) 'the integer value must be the line that you want to remove -1 Me.RichTextBox1.Text = String.Empty For i As Integer = 0 To lst.Items.Count - 1 If Me.RichTextBox1.Text = String.Empty Then Me.RichTextBox1.Text = lst.Items.Item(i) Else MeMe.RichTextBox1.Text = Me.RichTextBox1.Text & Environment.NewLine & lst.Items.Item(i).ToString End If Next End Sub
https://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/63647481-743d-4e55-9043-e0db5106a03a/
我不知道是否有一个简单的方法可以一步到位。 您可以在富文本框的.Text属性上使用.Split函数来获取行数组
string[] lines = richTextBox1.Text.Split( "n".ToCharArray() )
然后编写一些内容,在删除所需的行后将数组重新组合成单个文本字符串,并将其复制回富文本框的.Text属性。
这是一个简单的例子:
string[] lines = richTextBox1.Text.Split("n".ToCharArray() ); int lineToDelete = 2; //O-based line number string richText = string.Empty; for ( int x = 0 ; x < lines.GetLength( 0 ) ; x++ ) { if ( x != lineToDelete ) { richText += lines[ x ]; richText += Environment.NewLine; } } richTextBox1.Text = richText;
如果您的富文本框的行数超过10行,那么最好使用StringBuilder而不是字符串来组成新文本。
在文本范围内找到要删除的文本,在此处找到将文本设置为空,现在它已从文档中消失。
这是我的unit testing实现。
public static void DeleteLine([NotNull] this RichTextBox pRichTextBox, int pLineIndex) { if (pLineIndex < 0 || pLineIndex >= pRichTextBox.Lines.Length) throw new InvalidOperationException("There is no such line."); var start = pRichTextBox.GetFirstCharIndexFromLine(pLineIndex); var isLastLine = pLineIndex == pRichTextBox.Lines.Length - 1; var nextLineIndex = pLineIndex + 1; var end = isLastLine ? pRichTextBox.Text.Length - 1 : pRichTextBox.GetFirstCharIndexFromLine(nextLineIndex) - 1; var length = end - start + 1; pRichTextBox.Text = pRichTextBox.Text.Remove(start, length); }
unit testing:
(使用n
而不是Environment.NewLine
因为至少对我来说RTB会自动替换rn
而只是n
)
[TestMethod] public void TestDeleteLine_SingleLine() { var rtb = new RichTextBox(); rtb.Text = "This is line1.n"; rtb.DeleteLine(0); var expected = ""; Assert.AreEqual(expected, rtb.Text); } [TestMethod] public void TestDeleteLine_BlankLastLine() { var rtb = new RichTextBox(); rtb.Text = "n"; rtb.DeleteLine(1); var expected = "n"; Assert.AreEqual(expected, rtb.Text); } [TestMethod] public void TestDeleteLine_SingleLineNoEOL() { var rtb = new RichTextBox(); rtb.Text = "This is line1."; rtb.DeleteLine(0); var expected = ""; Assert.AreEqual(expected, rtb.Text); } [TestMethod] public void TestDeleteLine_FirstLine() { var rtb = new RichTextBox(); rtb.Text = "This is line1.nThis is line2.nThis is line3."; rtb.DeleteLine(0); var expected = "This is line2.nThis is line3."; Assert.AreEqual(expected, rtb.Text); } [TestMethod] public void TestDeleteLine_MiddleLine() { var rtb = new RichTextBox(); rtb.Text = "This is line1.nThis is line2.nThis is line3."; rtb.DeleteLine(1); var expected = "This is line1.nThis is line3."; Assert.AreEqual(expected, rtb.Text); } [TestMethod] public void TestDeleteLine_LastLine() { var rtb = new RichTextBox(); rtb.Text = "This is line1.nThis is line2.nThis is line3."; rtb.DeleteLine(2); var expected = "This is line1.nThis is line2.n"; Assert.AreEqual(expected, rtb.Text); } [TestMethod] public void TestDeleteLine_OneBlankLine() { var rtb = new RichTextBox(); rtb.Text = "n"; rtb.DeleteLine(0); var expected = ""; Assert.AreEqual(expected, rtb.Text); } [TestMethod] public void TestDeleteLine_BlankLines() { var rtb = new RichTextBox(); rtb.Text = "nnnnn"; rtb.DeleteLine(2); var expected = "nnnn"; Assert.AreEqual(expected, rtb.Text); } [TestMethod, ExpectedException(typeof(InvalidOperationException))] public void TestDeleteLine_Exception_BeforeFront() { var rtb = new RichTextBox(); rtb.Text = "nnnnn"; rtb.DeleteLine(-1); } [TestMethod, ExpectedException(typeof(InvalidOperationException))] public void TestDeleteLine_Exception_AfterEnd() { var rtb = new RichTextBox(); rtb.Text = "nn"; rtb.DeleteLine(3); }
很多很好的答案,但我觉得很多很复杂。
string[] LinesArray = this.richTextBox1.Lines; this.richTextBox1.Clear(); for (int line = 0; line < LinesArray.Length; line++) { if (!LinesArray[line].Contains("< Test Text To Remove >")) { this.richTextBox1.AppendText(LinesArray[line] + Environment.NewLine); } }
我希望这有助于其他人; 0)
上述就是C#学习教程:删除.NET RichTextBox中的特定行分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注---计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1005239.html