Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete a specific line in a .NET RichTextBox

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

like image 826
leeeroy Avatar asked Aug 25 '09 16:08

leeeroy


4 Answers

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.

like image 21
TLiebe Avatar answered Oct 29 '22 19:10

TLiebe


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);
}
like image 168
tomanu Avatar answered Oct 29 '22 19:10

tomanu


This also could do the trick (if you can handle things such as ++ in forms code). Keeps the text format. Just remember "ReadOnly" attribute work for both you and user.

richTextBox.SelectionStart = richTextBox.GetFirstCharIndexFromLine(your_line);
richTextBox.SelectionLength = this.richTextBox.Lines[your_line].Length+1;
this.richTextBox.SelectedText = String.Empty;
like image 25
wondra Avatar answered Oct 29 '22 19:10

wondra


Try this:

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

http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/63647481-743d-4e55-9043-e0db5106a03a/

like image 26
Alex Avatar answered Oct 29 '22 20:10

Alex