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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.