• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 985
  • Last Modified:

How do I programmatically add lines to a Rich Text Box

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.
0
jindalee
Asked:
jindalee
  • 2
  • 2
  • 2
  • +1
3 Solutions
 
Wayne Taylor (webtubbs)Commented:
Like this....

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

Wayne
0
 
manlimnagCommented:
Also you can use Append method I am not sure whether it is with text or at object level
0
 
Wayne Taylor (webtubbs)Commented:
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)

Wayne
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
manlimnagCommented:
No I suppose append don't require newline.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
0
 
jindaleeAuthor Commented:
Hello

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
Avery
        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

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
"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)
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.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now