|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
The Span Method Besides the SelRTF property, you can use the Span() method to select text based on a set of characters: RichTextBox.Span 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. You use Span() to extend a selection from the current insertion point based on a set of specified characters. This method searches the text in the rich text box (forwards or backwards as youve specified) and extends the text selection to include (or exclude, if youve so specified) as many of the characters youve specified in the character set that it can find. For example, to select the text from the current insertion point to the end of the sentence, use Span(.?!), which works for sentences ending in periods, question marks, or exclamation marks. Heres an example where we use Span() to find the word underlined and underline it:
Private Sub Command1_Click()
RichTextBox1.Text = "This rich text box supports underlined, bold, _
italic, and strikethru text."
RichTextBox1.SelStart = RichTextBox1.Find("underlined")
RichTextBox1.Span ("underlined")
RichTextBox1.SelUnderline = True
End Sub
Using Bold, Italic, Underline, And StrikethruTo make text bold, italic, underlined, or strikethru, you use the SelBold, SelItalic, SelUnderline, and SelStrikethru properties. These properties work on selected RTF text only, so you have to select the text whose format you want to change. To make this clearer, heres an example where we set the underline, bold, italic, and strikethru properties of text. We start by placing some text into a rich text box:
Private Sub Command1_Click()
RichTextBox1.Text = "This rich text box supports underlined, bold,_
italic, and strikethru text."
...
Next, well underline the word underlined in the text. We start by finding that word using the rich text box Find() method:
Private Sub Command1_Click()
RichTextBox1.Text = "This rich text box supports underlined, bold,_
italic, and strikethru text."
RichTextBox1.SelStart = RichTextBox1.Find("underlined")
...
We then use Span() to select the word underlined:
Private Sub Command1_Click()
RichTextBox1.Text = "This rich text box supports underlined, bold,_
italic, and strikethru text."
RichTextBox1.SelStart = RichTextBox1.Find("underlined")
RichTextBox1.Span ("underlined")
...
Finally, we underline the selected text by setting the rich text boxs SelUnderline property to True:
Private Sub Command1_Click()
RichTextBox1.Text = "This rich text box supports underlined, bold,_
italic, and strikethru text."
RichTextBox1.SelStart = RichTextBox1.Find("underlined")
RichTextBox1.Span ("underlined")
RichTextBox1.SelUnderline = True
...
And we can do the same to demonstrate bold, italic, and strikethru text:
Private Sub Command1_Click()
RichTextBox1.Text = "This rich text box supports underlined, bold,_
italic, and strikethru text."
RichTextBox1.SelStart = RichTextBox1.Find("underlined")
RichTextBox1.Span ("underlined")
RichTextBox1.SelUnderline = True
RichTextBox1.SelStart = 0
RichTextBox1.SelStart = RichTextBox1.Find("bold")
RichTextBox1.Span ("bold")
RichTextBox1.SelBold = True
RichTextBox1.SelStart = 0
RichTextBox1.SelStart = RichTextBox1.Find("italic")
RichTextBox1.Span ("italic")
RichTextBox1.SelItalic = True
RichTextBox1.SelStart = 0
RichTextBox1.SelStart = RichTextBox1.Find("strikethru")
RichTextBox1.Span ("strikethru")
RichTextBox1.SelStrikeThru = True
End Sub
Running this program yields the results you see in Figure 6.9.
Indenting Text In Rich Text BoxesOne of the aspects of word processors that users have gotten used to is the ability to indent text, and rich text boxes (which are designed to be RTF word processors in a control) have this capability. To indent paragraph-by-paragraph, you use these properties (you set them to numeric values to indicate the indentation amount, using the measurement units of the underlying form, which is usually twips):
To use these properties on a paragraph of text, you either select the paragraph (using SelStart and SelLength, or Span()), or simply place the insertion point in the paragraph (you can move the insertion point under program control with the UpTo() method). Heres an example: When the user places the insertion point in a paragraph of text and clicks a button, Command1, we can indent the paragraph 500 twips. We can then outdent all lines after the first by 250 twips with respect to the overall 500-twip indentation (which means that all lines after the first will be indented 250 twips from the left margin) and set the right indent to 100 twips:
Private Sub Command1_Click()
RichTextBox1.SelIndent = 500
RichTextBox1.SelHangingIndent = -250
RichTextBox1.SelRightIndent = 100
End Sub
Running this code on a paragraph of text yields the result you see in Figure 6.10. Now were indenting individual paragraphs in rich text controls.
Besides working paragraph-by-paragraph, you can set the right margin for the whole rich text at once with the RightMargin property. Just assign this property the new value you want for the right margin, and youre set.
|
|
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. |