Delete a specific line in a .NET RichTextBox

2020-04-10 06:04发布

How can I delete a specific line of text in a RichTextBox ?

9条回答
够拽才男人
2楼-- · 2020-04-10 07:01

Here is my unit tested implementation.

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 tests:

(used \n instead of Environment.NewLine since at least for me RTB is automatically replacing \r\n with just \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 = "\n\n\n\n\n";
   rtb.DeleteLine(2);
   var expected = "\n\n\n\n";
   Assert.AreEqual(expected, rtb.Text);
}

[TestMethod, ExpectedException(typeof(InvalidOperationException))]
public void TestDeleteLine_Exception_BeforeFront() {
   var rtb = new RichTextBox();
   rtb.Text = "\n\n\n\n\n";
   rtb.DeleteLine(-1);
}

[TestMethod, ExpectedException(typeof(InvalidOperationException))]
public void TestDeleteLine_Exception_AfterEnd() {
   var rtb = new RichTextBox();
   rtb.Text = "\n\n";
   rtb.DeleteLine(3);
}
查看更多
Emotional °昔
3楼-- · 2020-04-10 07:02

Another solution:

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);
}
查看更多
祖国的老花朵
4楼-- · 2020-04-10 07:03

I don't know if there is an easy way to do it in one step. You can use the .Split function on the .Text property of the rich text box to get an array of lines

string[] lines = richTextBox1.Text.Split( "\n".ToCharArray() )

and then write something to re-assemble the array into a single text string after removing the line you wanted and copy it back to the .Text property of the rich text box.

Here's a simple example:

        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;

If your rich text box was going to have more than 10 lines or so it would be a good idea to use a StringBuilder instead of a string to compose the new text with.

查看更多
登录 后发表回答