[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How do I programmatically add lines to a Rich Text Box

Posted on 2009-02-20
7
Medium Priority
?
981 Views
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.
0
Comment
Question by:jindalee
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 48

Expert Comment

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

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

Wayne
0
 
LVL 3

Expert Comment

by:manlimnag
ID: 23690558
Also you can use Append method I am not sure whether it is with text or at object level
0
 
LVL 48

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 23690689
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:manlimnag
ID: 23691040
No I suppose append don't require newline.
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 2000 total points
ID: 23692903
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
 
LVL 1

Accepted Solution

by:
jindalee earned 0 total points
ID: 23697476
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
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 2000 total points
ID: 23697665
"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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

873 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