The Windows forms RichTextBox control is used for displaying, entering, and manipulating rich text with formatting. The RichTextBox control does everything the TextBox control does, but in addition, it can display fonts, colors, and links; load text and embedded images from a file; undo and redo editing operations; and find specified characters.
Rich text format (RTF) text supports a variety of formats. For example, you can color text in a rich text box, underline it, bold it, or make it italic. You can select fonts and fonts sizes, as well as write the text out to disk or read it back in. Rich text boxes also can hold a great amount of data, unlike standard text boxes. Like standard text boxes, they're derived from the TextBoxBase class:
Object MarshalByRefObject Component Control TextBoxBase RichTextBox
RTF text was designed to be a step beyond plain text, and because many word processors let you save text in the rich text format, it can provide a link between different types of word processors. Using rich text boxes, you also can create your own word processors. Rich text boxes are used to support text manipulation and display features similar to the big-time word processors such as Microsoft Word. Like the TextBox control, the RichTextBox control can display scroll bars.
As with the TextBox control, the text displayed is set by the Text property. The RichTextBox control has many properties to format text, and we'll explore them here. You can set font attributes, set indents, create hanging indents, create bulleted paragraphs, and more. To work with files, you can use the LoadFile and SaveFile methods. You can even use a RichTextBox for Web-style links by setting the DetectUrls property to True and writing code to handle the LinkClicked event. And you can undo and redo most edit operations by calling the Undo and Redo methods; the CanRedo method will let you know if the last operation the user has undone can be reapplied.
Although plenty of Visual Basic programmers know about the rich text control, very few of them actually know how to format text in these controls; we'll see how to do that in this chapter. (As you might expect, it's changed totally from the way you used to do it in VB6.)
Tip |
To format text in a rich text box, you first select text, so here's a tip for VB6 programmers—selection properties that began with Sel now begin with Selection. |