שתף באמצעות


How to Delete lines in a Richtextbox

Question

Monday, June 30, 2008 4:34 PM

I have this richtextbox which get its text from an online html source, but I don't need the last three lines, so is there a way to get the three last lines and delete them?

Thank you ;)

All replies (7)

Monday, June 30, 2008 5:43 PM ✅Answered

Jake26 said:

I have this richtextbox which get its text from an online html source, but I don't need the last three lines, so is there a way to get the three last lines and delete them?

Thank you ;)

Try this using a hidden listbox:

1 Dim lst As New ListBox  
2     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click  
3         Me.Controls.Add(lst)  
4         For Each cosa As String In Me.RichTextBox1.Lines  
5             lst.Items.Add(cosa)  
6         Next  
7         lst.Items.RemoveAt(2) 'the integer value must be the line that you want to remove -1  
8         Me.RichTextBox1.Text = String.Empty  
9         For i As Integer = 0 To lst.Items.Count - 1  
10             If Me.RichTextBox1.Text = String.Empty Then  
11                 Me.RichTextBox1.Text = lst.Items.Item(i)  
12             Else  
13                 MeMe.RichTextBox1.Text = Me.RichTextBox1.Text & Environment.NewLine & lst.Items.Item(i).ToString  
14             End If  
15         Next  
16     End Sub 

Hope it helps.

Best regards.

Ale N.


My Website --> http://www.alession.blogspot.com/ || Please, write any comment or suggestion.


Monday, June 30, 2008 6:55 PM

Thanks, I guess that will work, but issent there a simpler way to do it? I just like to use simple solutions.. I will go for this for now, thank you ;) 


Tuesday, July 1, 2008 10:01 AM | 1 vote

 Try one of these approach. You might have to add validation whether the linetoberemoved is existing in the richtextbox.

        Dim lineToBeRemoved As Integer = 1 
        lineToBeRemovedlineToBeRemoved = lineToBeRemoved - 1  
        Dim str As String = RichTextBox1.Lines(lineToBeRemoved)  
        RichTextBox1.Find(str & vbCr)  
        RichTextBox1.SelectedText = "" 
 
        Dim richtxt As String = RichTextBox1.Text  
        Dim pos As Integer = richtxt.IndexOf(vbLf)  
        RichTextBox1.Text = richtxt.Substring(pos + 1, richtxt.Length - pos - 1) 

Friday, July 4, 2008 4:09 PM

Thank you both :D All I realy needed was the last two lines, dunno why I could not do this myself but this is the code I needed.

'//Make last three lines into one 
tempeditort.Find("</div></body></html>") 
tempeditort.SelectedText = "" 

Thursday, June 20, 2013 12:37 PM

You can write it better as below..

       Dim lineToBeRemoved As Integer = 1
        lineToBeRemoved = lineToBeRemoved - 1
        Dim str As String = RichTextBox1.Lines(lineToBeRemoved)
        MsgBox(str)
        Dim indexToText As Integer = RichTextBox1.Find(str & vbCr, 0, RichTextBoxFinds.WholeWord)
        If indexToText > -1 Then
            RichTextBox1.SelectedText = ""
        End If

Thursday, June 20, 2013 1:05 PM

Not sure why you wanted to update such an old thread, but the following seems a simpler way of removing the last 3 lines from a RichTextBox:

RichTextBox1.Lines = RichTextBox1.Lines.Take(RichTextBox1.Lines.Count - 3).ToArray

Monday, June 11, 2018 7:34 PM

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        RTB.Select(RTB.GetFirstCharIndexFromLine(2), RTB.Lines(2).Count)
        RTB.SelectionLength = RTB.Lines(2).Length + 1
        RTB.SelectedText = ""
    End Sub