Delete a specific line in a .NET RichTextBox
24,460
Solution 1
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);
}
Solution 2
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;
Solution 3
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/
Solution 4
Based on tomanu's solution but without overhead
int start_index = LogBox.GetFirstCharIndexFromLine(linescount);
int count = LogBox.GetFirstCharIndexFromLine(linescount + 1) - start_index;
LogBox.Text = LogBox.Text.Remove(start_index, count);
note that my linescount here is linescount - 2.
Solution 5
Find the text to delete in a text range, found here Set the text to empty, and now it is gone form the document.
Author by
leeeroy
Updated on July 09, 2022Comments
-
leeeroy almost 2 years
How can I delete a specific line of text in a RichTextBox ?
-
David Basarab over 14 yearsThis would get slow as the number of lines increased.
-
TLiebe over 14 yearsYou're right and I should have put a warning in there but for a reasonable number of lines (less than 100?) it should be fine.
-
Cary Bondoc over 8 yearsLink only answer is not good. :D Edit it and I'll up vote you Sir.
-
Dave Cousineau over 8 yearsdoes this work for the last line in the box? I like tomanu's
int count = richTextBox.Lines[a_line].Length;
line better. -
Dave Cousineau over 8 yearswhat does the "eat new line chars" block do? doesn't the -1 +1 cancel out? it would seem to evaluate to
count += 0
actually... -
Dave Cousineau over 8 yearsok I get it, the length doesn't otherwise include the end of line characters, missed that.
-
Dave Cousineau over 8 yearsrunning it through my tests, no it doesn't, though it might be related to what you say in the last line, not sure what that means.
-
Wesley Long over 8 yearsI tried this once. It bit me by losing my RTF formatting.
-
Tombala over 7 yearsThis removes all formatting from the existing text. If you don't want to remove formatting, you have to go with the route of "Create a selection, then clear the selection". See other answers.
-
Tombala over 7 yearsThis is the only approach that does not remove formatting from existing text. Setting the Text property clears all existing formatting. Also to note, if your textbox is readonly, set Readonly to false just before modifying SelectedText, then restore Readonly to true after it.
-
David Buck over 4 yearsWhen answering an old question, your answer would be much more useful to other StackOverflow users if you included some context to explain how your answer helps. See: How do I write a good answer.