I'm working on a project in vb.net 2010 that has 3 RichTextBoxes. Two of the RichTextBoxes can be loaded with text from text files. How do I take the multiple lines of text from each RichTextBox and combine them into the 3rd RichTextBox.

For example, if 2 of the RichTextBoxes contain the following lines of text:

(RichTextBox1)
Monday
Tuesday
Wednesday

(RichTextBox2)
Jan
Feb
Mar

I want the 3rd RichTextBox to show following result:

(RichTextBox3)
MondayJan
TuesdayFeb
WednesdayMar

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim FirstBox() As String = RichTextBox1.Lines
    Dim SecondBox() As String = RichTextBox2.Lines

    If FirstBox.Count <> SecondBox.Count Then Exit Sub

    For i = 0 To UBound(FirstBox)
        RichTextBox3.Text &= FirstBox(i) & " " & SecondBox(i) & vbCrLf
    Next

End Sub

If this solves your problem, mark it as solved. If not say why it didn't solve your problem.

Thank you very much. The code worked in combining the text lines, but the results were mixed in that several of the text lines have gaps (trailing spaces & tab spaces) between the text. I tried using the Trim Method, but it didn't seem to work.

RichTextBox3.Text &= Trim(firstbox(i)) & Trim(secondbox(i)) & vbCrLf

This is the result I got:
DARELL 03752
BRODERICK 03753
ALONSO03754

Edited 5 Years Ago by mikeylala: n/a

Hi,
use this for concating two richtextbox into single richtextbox

RichtextTextBox3.SelectedRtf = RichtextTextBox1.Rtf
RichtextTextBox3.SelectedRtf = RichtextTextBox2.Rtf

Try this to trim your tabs and spaces.

RichTextBox3.Text &= FirstBox(i).Trim & " " & SecondBox(i).Trim & vbCrLf

It's not the same thing you have.


The above code will join the text, but won't combine the lines.

Thank you, that worked great. The program is producing the desired results now. You all have been awesome... thank you so much.

this one also usefull

        richTextBox1.SelectAll();
        richTextBox1.Copy();
        richTextBox3.Paste();

        richTextBox2.SelectAll();
        richTextBox2.Copy();
        richTextBox3.Paste();

Edited 2 Years Ago by Dani: Formatting fixed

This question has already been answered. Start a new discussion instead.