Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding to Rich Text in Rich TextBox 1

Status
Not open for further replies.

barrytraver

Programmer
Nov 28, 2000
40
0
0
US
I'm trying to figure out how to modify rich text in a rich textbox in code.

Perhaps an example is the best way to show what I do and don't know how to do.

Let's suppose I have the plain text "one two three " in a Rich Textbox.

I know how to take the word "three" and change the color to vbRed and the font style to bold. (It's simply a matter of setting Text1.SelStart, Text1.SelLength, Text1.SelColor nd Text1.SelBold.) So far, so good.

What I don't know how to do is add a word at the end without losing the RTF information in the presently-existing string.

That is, I can add &quot;four &quot; to the end so that the text in the text box says &quot;one two three four &quot;, but when I do that, the red and bold characteristics of the word &quot;three&quot; disappear. The whole string is now normal black type (no red, no bold). <sigh>

Any help on this would be greatly appreciated. Thanks in advance.

Barry Traver
 
P.S. I've experimented with various combinations of

Text1.Text = Text1.Text & NewWord

and

Text1.TextRTF = Text1.TextRTF & NewWord

but I haven't found one that works to add a new word at the end of text in a rich txtbox without losing the formatting (color, bold, etc.) of the existing text in the textbox.

Something I haven't tried yet is to use SendKeys to send the letters of the new word one by one, but I would think that there should be a more direct way to accomplish the task of adding new material to rich text in a rich textbox without losing the special RTF formatting of what is already there.

Again, any advice would be greatly appreciated. Thanks!
 
I had the same problem and I found a way to work around:

Set the SelStart Property to the End of your RichTxt and then add the new text with SelText = newTXT. I don't know if this is the best solution, but at least it works ...

CU
Tom
 
Tom,

You're right: it works, and it works well! And you can use this technique to add text anywhere in the richtextbox (not just at the end) without messing up the presently-existing formatting.

Thanks for the tip.

Barry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top