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 Fonts And Font Sizes In Rich Text Boxes

Another call from the Field Testing Department. It seems that the users want to use different fonts in your word-processor program. Well, some people are never satisfied—but rich text boxes can help here, too.

To set a selection’s font, you just set the SelFontName to the new font name (for example, Arial or Times New Roman). To set a selection’s font size, you just set the SelFontSize property. That’s all it takes.

Here’s an example. In this case, we’ll display the text “This rich text box supports fonts like Arial and Courier in different sizes.” in a rich text box, and format the words “Arial” and “Courier” in those fonts, and in different font sizes.

We start by placing that text in a rich text box:

Private Sub Command1_Click()
    RichTextBox1.Text = "This rich text box supports fonts like Arial and_
        Courier in different sizes."
...

Next, we select the word “Arial”:

Private Sub Command1_Click()
    RichTextBox1.Text = "This rich text box supports fonts like Arial and_
        Courier in different sizes."

    RichTextBox1.SelStart = RichTextBox1.Find("Arial")
    RichTextBox1.Span ("Arial")
...

Then we display that word in Arial font, with a 24-point size:

Private Sub Command1_Click()
    RichTextBox1.Text = "This rich text box supports fonts like Arial and_
        Courier in different sizes."

    RichTextBox1.SelStart = RichTextBox1.Find("Arial")
    RichTextBox1.Span ("Arial")
    RichTextBox1.SelFontName = "Arial"
    RichTextBox1.SelFontSize = 24
...

We do the same for the word “Courier”, displaying it in 18-point size:

Private Sub Command1_Click()
    RichTextBox1.Text = "This rich text box supports fonts like Arial and_
        Courier in different sizes."

    RichTextBox1.SelStart = RichTextBox1.Find("Arial")
    RichTextBox1.Span ("Arial")
    RichTextBox1.SelFontName = "Arial"
    RichTextBox1.SelFontSize = 24

    RichTextBox1.SelStart = 0
    RichTextBox1.SelStart = RichTextBox1.Find("Courier")
    RichTextBox1.Span ("Courier")
    RichTextBox1.SelFontName = "Courier"
    RichTextBox1.SelFontSize = 18
End Sub

The result appears in Figure 6.11.


Figure 6.11  Setting fonts and font sizes.

Being able to set the font and font size of individual text selections instead of working with all the text at once in a rich text box is a very powerful capability.

Using Bullets In Rich Text Boxes

Rich text boxes support bullets, those black dots that appear in lists of items that you want to set off in text. Putting a bullet in front of each item gives the list a snappy appearance and helps the reader assimilate the information quickly.

To set bullets, you use the SelBullet and BulletIndent properties. The SelBullet property displays a bullet in front of the paragraph in which the current selection is; the BulletIndent property indicates how much you want the bullet to be indented from the left.


TIP:  It’s a good idea to set the bullet indentation, because if you don’t, the bullet will appear right in front of the first character in the paragraph you’re bulleting, which can look awkward.

Let’s make this clearer with an example. We start by placing some text in a rich text box:

Private Sub Command1_Click()
    RichTextBox1.Text = "This rich text box shows how to use bullets _
        and indent bulleted text."
...

We set the indentation for this paragraph to 200 twips:

Private Sub Command1_Click()
    RichTextBox1.Text = "This rich text box shows how to use bullets _
        and indent bulleted text."
    RichTextBox1.SelIndent = 200
...

Next, we set the bullet’s indent to 90 twips, so it’s set off from the rest of the text. We set that indent with the BulletIndent property:

Private Sub Command1_Click()
    RichTextBox1.Text = "This rich text box shows how to use bullets _
        and indent bulleted text."
    RichTextBox1.SelIndent = 200
    RichTextBox1.BulletIndent = 90
...

Finally, we add the bullet with the SelBullet property:

Private Sub Command1_Click()
    RichTextBox1.Text = "This rich text box shows how to use bullets _
        and indent bulleted text."
    RichTextBox1.SelIndent = 200
    RichTextBox1.BulletIndent = 90
    RichTextBox1.SelBullet = True
End Sub

That’s it—the result appears in Figure 6.12.


Figure 6.12  Adding a bullet to text in a rich text box.

Aligning Text In A Rich Text Box

You can set the alignment of text in a rich text box paragraph-by-paragraph using the SelAlignment property. You just select the paragraph you want to align, or place the insertion point in that paragraph, and set the SelAlignment property to one of the following values:

  rtfLeft—0(the default); the paragraph is aligned along the left margin.
  rtfRight—1; the paragraph is aligned along the right margin.
  rtfCenter—2; the paragraph is centered between the left and right margins.

Being able to align text paragraph-by-paragraph like this is much more powerful than the simple Alignment property of a standard text box, which aligns all the text at the same time.


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.