Delete a specific line in a .NET RichTextBox

后端 未结 9 1526
后悔当初
后悔当初 2021-01-19 15:08

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

9条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-19 15:35

    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);
    }
    

提交回复
热议问题