VB.net Read contents of a RichTextBox line by line

Hi

I have a RichTextBox that users populate using buttons with code like that below
What VB.net code do I use to loop through multiple lines?

  Private Sub btnAddRange_Click(sender As System.Object, e As System.EventArgs) Handles btnAddRange.Click
        Try
            Me.RichTextBox1.Text = Me.RichTextBox1.Text & " " & Me.txtRange.Text
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub btnNewLine_Click(sender As System.Object, e As System.EventArgs) Handles btnNewLine.Click
        Try
            Me.RichTextBox1.Text = Me.RichTextBox1.Text & " " & vbCrLf
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?
 
Fernando SotoRetiredCommented:
Hi murbro;

The RichTextBox has a Lines property which is an array of String which can be indexed or enumerated through to get each individual line in the control.

Dim line4 As String = RichTextBox1.Lines(3)
0
 
Thunder724Commented:
Can you not just load the file into the RichTextBox?
RichtextBox1.Load("file path/name")
0
 
Thunder724Commented:
Sorry, made a mistake in the code....
RichtextBox1.LoadFile("file path/name")

Open in new window

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Fernando SotoRetiredCommented:
@Thunder724

 The user of the application selects a button and then a value is added to the RichTextBox, all at run time, dynamically.
0
 
Thunder724Commented:
You can put each line into an array and then reference the array..

DIM RTFLines() As String
RTFLines = SPLIT(RichTextBox1.Text, VbCrLF)

'Loop through each row now in the array adding to a Listbox (example)
FOR X AS INT16 = 0 TO UBOUND(RTFLines) -1
    ListBox1.Add(RTFLines(x).ToString)
NEXT

Open in new window

0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks a lot for the help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.