How do I programmatically add lines to a Rich Text Box

Posted on 2009-02-20
Last Modified: 2013-11-27
I have to read the contents of a number of text boxes andmaybe some list boxes (which could potentially hold a large number of items).

Having read them I have to add them to a Rich Text Box for output to a file or printer (or both).

I'm pretty new to Visual BAsic and have learned how to do things with RTBs like change fonts, styles and change the current text.

Can somone please advise me how I write multiple lines to a RTB, i.e. read the contents of a text box, insert it into the next line of a RTB, read the next text box, insert it etc.

(I know how to get the contents of the txt and list boxes, just not how to add them all to the RTB.
Question by:jindalee
    LVL 46

    Expert Comment

    by:Wayne Taylor (webtubbs)
    Like this....

        RichTextBox1.Text &= "Your new text" & ControlChars.NewLine

    LVL 3

    Expert Comment

    Also you can use Append method I am not sure whether it is with text or at object level
    LVL 46

    Expert Comment

    by:Wayne Taylor (webtubbs)
    Yes, the AppendText method works the same as I posted. Either way, the ControlChars.NewLine constant is needed to add a new line.....

        RichTextBox1.AppendText("Your new text" & ControlChars.NewLine)

    LVL 3

    Expert Comment

    No I suppose append don't require newline.
    LVL 85

    Assisted Solution

    by:Mike Tomlinson
    If your text is FORMATTED (colored, bold, fonts, etc.) then do NOT use Waynes first suggestion.  Setting the .Text() Property REMOVES any existing formatting in the RTB.

    Yet another way to append text to the end of an RTB:

            ' move insertion point to the end
            RichTextBox1.SelectionStart = RichTextBox1.TextLength
            RichTextBox1.SelectionLength = 0

            ' optional: Set some formatting
            RichTextBox1.SelectionColor = Color.Red
            ' etc...

            ' add some text
            RichTextBox1.SelectedText = "(" & DateTime.Now & ") some message..." & vbCrLf
    LVL 1

    Accepted Solution


    Thank you for the suggestions. Idle_mind's suggestion is the better one for the reasons stated.

    One more question on this if I may. If I put one line of text in with one formatting style, then add a second with a different formatting, all of the text is displayed in the second format. Why would this be?

    I've attached a code snippet.

    Thank you
            myRTB.Font = New Font("Arial", 16, FontStyle.Bold)
            myRTB.Text = "Some text" & vbCrLf
            myRTB.SelectionStart = myRTB.TextLength
            myRTB.SelectionLength = 0
            myRTB.Font = New Font("Arial", 12, FontStyle.Regular)
            myRTB.AppendText("Some more text")

    Open in new window

    LVL 85

    Assisted Solution

    by:Mike Tomlinson
    "myRTB.Font" sets the font for EVERYTHING in the RTB.

    If you want to change just the text being Appended then use SelectionFont() instead:

        RichTextBox1.SelectionFont = New Font("Arial", 12, FontStyle.Regular)

    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    VB.Net - 2005 Project Not Responding 24 41
    Record open by another user 6 25
    Detect file exist or not 3 44
    Merging PDFs with VB.NET 10 13
    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now