The RichTextBox control is the core of a full-blown word processor. It provides all the functionality of a TextBox control; it can handle multiple typefaces, sizes, and attributes, and offers precise control over the margins of the text (see Figure 4.16). You can even place images in your text on a RichTextBox control (although you won’t have the kind of control over the embedded images that you have with Microsoft Word).
The fundamental property of the RichTextBox control is its Rtf property. Similar to the Text property of the TextBox control, this property is the text displayed on the control. Unlike the Text property, however, which returns (or sets) the text of the control but doesn’t contain formatting information, the Rtf property returns the text along with any formatting information. Therefore, you can use the RichTextBox control to specify the text’s formatting, including paragraph indentation, font, and font size or style.
RTF, which stands for Rich Text Format, is a standard for storing formatting information along with the text. The beauty of the RichTextBox control for programmers is that they don’t need to supply the formatting codes. The control provides simple properties to change the font of the selected text, change the alignment of the current paragraph, and so on. The RTF code is generated internally by the control and used to save and load formatted files. It’s possible to create elaborately formatted documents without knowing the RTF specification.
Figure 4.16 – A word processor based on the functionality of the RichTextBox control
The WordPad application that comes with Windows is based on the RichTextBox control. You can easily duplicate every bit of WordPad’s functionality with the RichTextBox control, as you will see later, in the section “The RTF Example Project”.
The RTF Language
A basic knowledge of the RTF format, its commands, and how it works will certainly help you understand the RichTextBox control’s inner workings. RTF is a language that uses simple commands to specify the formatting of a document. These commands, or tags, are ASCII strings, such as \par (the tag that marks the beginning of a new paragraph) and \b (the tag that turns on the bold style). And this is where the value of the RTF format lies. RTF documents don’t contain special characters and can be easily exchanged among different operating systems and computers, as long as there is an RTF-capable application to read the document. Let’s look at an RTF document in action.
Open the WordPad application (choose Start > Programs > Accessories > WordPad) and enter a few lines of text (see Figure 4.17). Select a few words or sentences, and format them in different ways with any of WordPad’s formatting commands. Then save the document in RTF format: Choose File > Save As, select Rich Text Format, and then save the file as Document.rtf. If you open this file with a text editor such as Notepad, you’ll see the actual RTF code that produced the document. A section of the RTF file for the document shown in Figure 4.17 is shown in Listing 4.20.
Figure 4.17 – The formatting applied to the text by using WordPad’s commands is stored along with the text in RTF format.
Listing 4.20: The RTF Code for the First Paragraph of the Document in Figure 4.17
{\rtf1\ansi\ansicpg1252\deff0\deflang1033
{\fonttbl{\f0\fnil\fcharset0 Verdana;}{\f1\fswiss\fcharset0 Arial;}}
\viewkind4\uc1\pard\nowidctlpar\fi720 \b\f0\fs18 RTF
\b0 stands for \i Rich Text Format\i0 ,
which is a standard for storing formatting
information along with the text. The beauty
of the RichTextBox control for programmers
is that they don\rquote t need to supply the
formatting codes. The control provides simple
properties that turn the selected text into bold,
change the alignment of the current paragraph, and so on.\par
Code language: JavaScript (javascript)
As you can see, all formatting tags are prefixed with the backslash (\) symbol. The tags are shown in bold to stand out in the listing. To display the \ symbol itself, insert an additional slash. Paragraphs are marked with the \par tag, and the entire document is enclosed in a pair of curly brackets. The \li and \ri tags that are followed by a numeric value specify the amount of the left and right indentation. If you assign this string to the RTF property of a RichTextBox control, the result will be the document shown in Figure 4.16, formatted exactly as it appears in WordPad.
RTF is similar to Hypertext Markup Language (HTML), and if you’re familiar with HTML, a few comparisons between the two standards will provide helpful hints and insight into the RTF language. Like HTML, RTF was designed to create formatted documents that could be displayed on different systems. The following RTF segment displays a sentence with a few words in italics:
\bRTF\b0 (which stands for Rich Text Format) is a \i
document formatting language\i0 that uses simple
commands to specify the formatting of the document.
Code language: JavaScript (javascript)
The following is the equivalent HTML code:
<b>RTF</b> (which stands for Rich Text Format) is a
<i>document formatting language</i> that uses simple
commands to specify the formatting of the document.
Code language: HTML, XML (xml)
The <b> and <i> tags of HTML, for example, are equivalent to the \b and \i tags of RTF. The closing tags in RTF are \b0 and \i0, respectively.
RTF, however, is much more complicated than HTML. It’s not nearly as easy to understand an RTF document as it is to understand an HTML document, because RTF was meant to be used internally by applications. As you can see in Listing 8.3, RTF contains information about the font being used, its size, and so on. Just as you need a browser to view HTML documents, you need an RTF-capable application to view RTF documents. WordPad, for instance, supports RTF and can both save a document in RTF format and read RTF files.