Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Setting Text Color In RTF Boxes

Another call from the Testing Department—now the users want to use different text colors in your word-processing program. Can you do that? Yes, you can, using the SelColor property.

To set colors in a rich text box, you just make a selection and set the rich text box’s SelColor property using the RGB() function. You pass three values (each ranging from 0 to 255) to the RGB() function for the three color values: red, green, and blue.

Here’s an example to make this clearer. We display the text “This rich text box supports font colors like red and blue and green.” in a rich text box, and color the word “red” red, “blue” blue, and “green” green. Here’s how that example looks in code:

Private Sub Command1_Click()
    RichTextBox1.Text = "This rich text box supports font colors like _
        red and blue and green."

    RichTextBox1.SelStart = RichTextBox1.Find("red")
    RichTextBox1.Span ("red")
    RichTextBox1.SelColor = RGB(255, 0, 0)

    RichTextBox1.SelStart = 0
    RichTextBox1.SelStart = RichTextBox1.Find("green")
    RichTextBox1.Span ("green")
    RichTextBox1.SelColor = RGB(0, 255, 0)

    RichTextBox1.SelStart = 0
    RichTextBox1.SelStart = RichTextBox1.Find("blue")
    RichTextBox1.Span ("blue")
    RichTextBox1.SelColor = RGB(0, 0, 255)
End Sub

This program produces the display you see in Figure 6.13. (Although it only appears in black and white in this book, the word red is red, and so on!)


Figure 6.13  Coloring text in a rich text box.

Moving The Insertion Point In RTF Boxes

Using the UpTo() method, you can move the insertion point around in a rich text box. This method moves the insertion point up to (but not including) a character or set of characters. Moving the insertion point yourself can be a powerful technique in a rich text box—for example, you can move the insertion point to a section of text the user is searching for. Here’s how the UpTo() method works:

RichTextBox.UpTo(characterset, forward, negate)

The characterset parameter is a string that specifies the set of characters to look for. The forward parameter determines which direction the insertion point moves. The negate parameter specifies whether the characters in characterset define the set of target characters or are excluded from the set of target characters.

This is made easier to understand with an example, so let’s put together an example now. Here, we’ll display the text “Click the button to move the insertion point here: *”, and when the user clicks a button, we’ll move the insertion point right up to the asterisk (*).

We begin by displaying that text in a rich text box when the form loads:

Private Sub Form_Load()
    RichTextBox1.Text = "Click the button to move the insertion point _
        here: *"
End Sub

Next, when the user clicks a button, we can move the insertion point up to the asterisk in the text this way (note, of course, that you can search for multi-character text as well as single characters):

Private Sub Command1_Click()
    RichTextBox1.UpTo ("*")
...
End Sub

That’s not quite good enough, though. Because we’ve clicked the command button, the button now has the focus, which means the blinking insertion point in the rich text box isn’t visible at all. To make sure the insertion point in the rich text box reappears, we give the focus back to the rich text box. This program appears in Figure 6.14. Now we’re handling the insertion point.


Figure 6.14  Moving the insertion point in a rich text box.

Private Sub Command1_Click()
    RichTextBox1.UpTo ("*")
    RichTextBox1.SetFocus
End Sub

Adding Superscripts And Subscripts In Rich Text Boxes

Uh oh—the users of your new word-processing program, SuperDuperTextPro, are demanding more text-formatting power. Your program has become so popular that the staff physicists are starting to use it, but they want to use superscripts and subscripts in text. Can you add that?

Yes, with the rich text box SelCharOffset property. You use this property to make a selection a superscript or subscript—if you set this value to a positive value, you get a superscript, and if you set it to a negative value, you get a subscript. (All measurements use the measurement units of the underlying form, such as twips.)

Let’s see an example. Here we can display a simple quadratic equation using this text

X12 + 2X1 + 1 = 0

where we’ll make the 1s subscripts and the first 2 a superscript. We start by displaying that text in a rich text box:

Private Sub Form_Load()
    RichTextBox1.Text = "X12 + 2X1 + 1 = 0"
End Sub

Next, we select the characters we want and set the SelCharOffset property to positive or negative twip values to create superscripts and subscripts:

Private Sub Command1_Click()
    RichTextBox1.UpTo ("1")
    RichTextBox1.Span ("1")
    RichTextBox1.SelCharOffset = ᇐ

    RichTextBox1.UpTo ("2")
    RichTextBox1.Span ("2")
    RichTextBox1.SelCharOffset = 40

    RichTextBox1.UpTo ("1")
    RichTextBox1.Span ("1")
    RichTextBox1.SelCharOffset = ᇐ
End Sub

That’s it—the result of this code appears in Figure 6.15. Now even the physicists will be happy.


Figure 6.15  Using superscripts and subscripts in a rich text box.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.