Click here to Skip to main content
15,610,483 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello guys

I want to ask how to split lines in Richtextbox.
I have a program that work as a client. The server sent client data


Ship Speed : 40 KM/H

Direction : North - West


the data is sent to client and displayed on Richtexbox at Client Form.

What i want to do right now is to split lines to textboxes


Text box 1 show 40 KM/H

Text box 2 show North - West


Can anyone help me? (sorry for bad grammar)
Hope my question is not confusing.

Thank you


This is my code i tried:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim tbarry() As String = RichTextBox1.Lines.Clone

        TextBox1.Text = tbarry(0)
        TextBox2.Text = tbarry(1)
        TextBox3.Text = tbarry(2)

it works when i try it in new project. But it crash when i tried to implement it to my main program

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If Listener.Pending = True Then
            Message = ""
            Client = Listener.AcceptTcpClient()

            Dim Reader As New StreamReader(Client.GetStream())
            Dim tbarry() As String = RichTextBox1.Lines.Clone
            While Reader.Peek > -1
                Message = Message + Convert.ToChar(Reader.Read()).ToString
            End While
            RichTextBox1.ForeColor = Color.Black
            RichTextBox1.Text += Message + vbCrLf
            RichTextBox1.Text = RichTextBox1.Text.Replace(RichTextBox1.Text, Message + vbCrLf)
            RichTextBox2.ForeColor = Color.Black
            RichTextBox2.Text += Message + vbCrLf
            RichTextBox2.Text = RichTextBox2.Text.Replace(RichTextBox2.Text, Message + vbCrLf)

            TextBox1.Text = tbarry(0)
            TextBox2.Text = tbarry(1)
            TextBox3.Text = tbarry(2)
        End If

    End Sub

it said "Index was outside the bounds of the array"
Updated 31-Jul-13 19:38pm
Sergey Alexandrovich Kryukov 31-Jul-13 23:44pm    
Start here:
imunksky 1-Aug-13 0:05am    
Sorry, and thanks for telling me. I have edited the post.
Sergey Alexandrovich Kryukov 1-Aug-13 0:14am    
No problem and thank you for understanding and clarifying the question. I tried to answer.

1 solution

It simply means that the RTF text, by some reason, has less then 3 lines. Run it under the debugger and you will see. Never assume number of elements in array or some collection. Before indexing of anything which is accessed by index, always check the number of elements.

Share this answer
imunksky 1-Aug-13 1:36am    
I finally get it work. Now i just have to extract the part of the text. Thank you for the advice +5
Sergey Alexandrovich Kryukov 1-Aug-13 8:14am    
Great. You are very welcome.
Good luck, call again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900