Adding to Rich Text in Rich Textbox 
Author Message
 Adding to Rich Text in Rich Textbox

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 "four " to the end so that the text in the text box
says "one two three four ", but when I do that, the red and bold
characteristics of the word "three" 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



Thu, 21 Aug 2003 21:44:51 GMT  
 Adding to Rich Text in Rich Textbox
Barry

You're halfway there with the Sel* properties, now look at the one with
Start in it, then work out the length of the text in the RTB and set this
property, then...

regards

Ian

** invalid email address, change dk to denmark

homepage http://www.kingsoft-denmark.com/
Tips & Tricks page http://tips.kingsoft-denmark.com/


Quote:
> 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 "four " to the end so that the text in the text box
> says "one two three four ", but when I do that, the red and bold
> characteristics of the word "three" 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



Thu, 21 Aug 2003 22:59:41 GMT  
 Adding to Rich Text in Rich Textbox

Quote:
> I'm trying to figure out how to modify rich text in a rich textbox in
> code.

You are probably adding text using something like "RichTextBox1.Text =
RichTextBox1.Text & "some more text". This will replace the existing text
with the new concatenated text, thus removing any existing text attributes.

Adding stuff to a RichTextBox in this way is not a good idea anyway, because
as the text grows in size it will become increasingly slow. What you need to
do is use the SelText property. This allows you to insert new text into the
existing text without altering any of the existing text or any of its
attributes. You can use this method to add new text anywhere at all within
the existing text, but to add it at the end you might use something like:

RichTextBox1.Text = "This is some text." ' The initial contents
RichTextBox1.SelStart = 8
RichTextBox1.SelLength = 4
RichTextBox1.SelColor = vbBlue
RichTextBox1.SelStart = Len(RichTextBox1.Text)
RichTextBox1.SelText = " Okay."

Mike



Fri, 22 Aug 2003 02:57:48 GMT  
 Adding to Rich Text in Rich Textbox
Mike,

Your analysis was right on target!.  I was indeed concatenating the
old text and the new text (and I couldn't get it to work, regardrless
of what variations I was using of

  RichTextBox1.Text = RichTextBox1.Text & "some more text"

and/or

  RichTextBox1.TextRTF = RichTextBox1.TextRTF & "some more text").

Your solution works beautifully (you must be a teacher in addition to
being a programmer, because the sample code was just what was needed
to make everything perfectly clear).

I'm very grateful for the help.  It was the only missing piece I
needed for a program I was working on (a study aid to help people
memorize text word-for-word).  Thanks again!

Barry



Fri, 22 Aug 2003 05:08:32 GMT  
 Adding to Rich Text in Rich Textbox

Quote:

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

I think you're stuck with setting .SelStart, .SelLength, .SelText,
and/or .SelRTF, though users might be less than ecstatic with the
jumping around that could result.

--

WARNING: I cannot be held responsible for the above        They're   coming  to
because  my cats have  apparently  learned to type.        take me away, ha ha!



Fri, 22 Aug 2003 10:06:47 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Becoming rich with a rich text box

2. A less rich Rich Text Field?

3. Adding text to Rich Text Box

4. Adding graphics to a Rich TextBox

5. text alignment in a rich textbox

6. Newbie - VB3 - looking for rich text textbox

7. Print text from a Rich TextBox

8. How to save a Rich TextBox.text into access database

9. HELP!! Rich TextBox Text to MS Word Problems

10. Rich textbox .selprint cuts off text

11. Printing Rich TextBox Text

12. VB6 Rich Textbox control (vertical alignment of text)

 

 
Powered by phpBB® Forum Software