none
RICHTEXTBOX LINES PROBLEMS RRS feed

  • Question

  • I want to delete the first two lines of text entries of a richtextbox

    if we have at least ten text entries in the richtextbox

    but my code does'nt work

    here is my code

       

    Dim lines As New List(Of String)(RichTextBox1.Lines)     

      If lines.Count > 10 Then     

      lines.Remove(1)     

      lines.Remove(2)       

     End If



    Tuesday, May 29, 2018 12:52 AM

All replies

  • if we have at least ten text entries in the richtextbox

    but my code does'nt work

    The lines that are visible in a rich text box may not correspond to lines of text - one is a visual display and the other depends on the characters in the string.  You need  to describe in more detail what the text in the text box actually is and what exactly you mean by 'doesn't work'.  Are you dealing with lines as displayed or lines as delimited in the underlying string?  Where is the Rich Text Box content coming from and how is it being loaded into the RTB? What do you mean by 'text entries'? 

    Tuesday, May 29, 2018 1:37 AM
  • Hi

    Here is one way.

    Example

        RTB.LoadFile(Path, RichTextBoxStreamType.PlainText)
    
        Dim lst As List(Of String) = RTB.Lines.ToList
        lst.RemoveRange(0, 2)
    
        RTB.Lines = lst.ToArray
    


    Regards Les, Livingston, Scotland

    Tuesday, May 29, 2018 1:38 AM
  • I want to delete the first two lines of text entries of a richtextbox

    if we have at least ten text entries in the richtextbox

    but my code does'nt work

    here is my code

       

    Dim lines As New List(Of String)(RichTextBox1.Lines)     

      If lines.Count > 10 Then     

      lines.Remove(1)     

      lines.Remove(2)       

     End If



    Well along with what Acamar says and the answer leshay provides your code will remove index 1 first then index 2 will become index 1 and then you remove index 2 which would be out of sequence. But you should be starting at index 0 to remove index 0 then 1 becomes 0 and you need to remove it.

    For you code it would be lines.Remove(0) twice to remove the first two lines from the List(Of String). Leshays code does that using the Range method by starting at index zero and removing the first two indexes zero and one which would be the first two items in the list of string.


    La vida loca

    Tuesday, May 29, 2018 2:04 AM
  • Two more possibilities:

       If RTB.Lines.Count > 10 Then RTB.Lines = RTB.Lines.Skip(2).ToArray

    And:

       If RTB.Lines.Count > 10 Then

          RTB.Select(0, RTB.GetFirstCharIndexFromLine(2))

          RTB.SelectedText = ""

       End If


    Tuesday, May 29, 2018 5:09 AM
  • Hi Jacob,

    Try the following code:

     Private Sub Frmrichtextbox_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            RichTextBox1.LoadFile("D:\Testfile\Test4.txt", RichTextBoxStreamType.PlainText)
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim linearray As String() = RichTextBox1.Lines
            If linearray.Count > 10 Then
                Dim linecollection = New List(Of String)(linearray)
                linecollection.RemoveRange(0, 2)
                RichTextBox1.Lines = linecollection.ToArray()
            End If
    
        End Sub

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 30, 2018 2:52 AM
    Moderator
  • @Cherry,

    Again, you are simply reposting what is essentially the same solution that has already been provided by others.  Why do you do this???


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, May 30, 2018 11:38 AM
    Moderator