How can I delete a specific line of text in a RichTextBox ?
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);
}
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/
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);
}
Lots of good answers, but I find many far to complicated.
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);
}
}
I hope this helps others some ;0)
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;
Find the text to delete in a text range, found here Set the text to empty, and now it is gone form the document.