Using the RichTextBox Control

Using the RichTextBox Control

The RichTextBox control is used to provide text editing support for text stored in Rich Text Format (RTF). The RTF standard was developed by Microsoft as an early specification for exchanging text that includes formatting information. The WordPad application distributed with Windows uses a Microsoft Win32 RichText control, which is the moral equivalent of the Windows Forms RichTextBox control.

The RichTextBox control shares a base class (TextBoxBase) with the TextBox control, which is discussed in Chapter 12. As with the TextBox control, text can be entered directly into the RichTextBox control by the user. Text can also be loaded into the control from a file or a stream, in either text or RTF format. Many of the properties and methods exposed by the TextBox control are also available through the RichTextBox control. The most commonly used properties include the following:

  • BackColor

  • ForeColor

  • Lines

  • Multiline

  • Scrollbars

  • WordWrap

Although these properties are shared by the TextBox and RichTextBox classes, in some cases the default values of the properties differ between the two control classes. For example, the default value for the Multiline property for a TextBox control is false, whereas the default value for a RichTextBox control is true. These differences reflect the typical usage scenarios for the controls. A TextBox control is often used for simple text input, and a RichTextBox control is typically used to contain text formatted with multiple styles.

Common Formatting Options

As it does for the TextBox control, the ForeColor property of the RichTextBox control specifies the color used to display text. Unlike the TextBox control, however, the RichTextBox control exposes properties that enable you to specify multiple formatting options for text. This feature enables you to create headings and bulleted lists and also allows you to emphasize sections of text using italic or boldface fonts. Two of the more commonly used properties that target selected text are listed here:

  • SelectionColor  Specifies the color used by the currently selected text

  • SelectionFont  Specifies the font used by the currently selected text

The following code uses the SelectionColor property to change the color for the currently selected text, using the ColorDialog class to obtain the color from the user:

private void editColorMenuItem_Click(object sender, System.EventArgs e)
{
    ColorDialog dlg = new ColorDialog();
    dlg.Color = richTextBox.SelectionColor;
    if(dlg.ShowDialog() == DialogResult.OK)
    {
        richTextBox.SelectionColor = dlg.Color;
    }
}

The following code uses the FontDialog class to get the user’s choice of font and then applies the font to the currently selected text with the SelectionFont property:

private void editFontMenuItem_Click(object sender, System.EventArgs e)
{
    FontDialog dlg = new FontDialog();
    dlg.Font = richTextBox.SelectionFont;
    if(dlg.ShowDialog() == DialogResult.OK)
    {
        richTextBox.SelectionFont = dlg.Font;
    }
}

Other properties enable you to specify the current list bullet style, text alignment, paragraph formatting, and other options. When no text is currently selected, new formatting options set using these properties will be applied to the current text insertion point, and any new text entered into the control will be formatted using the new properties.

When a text selection includes text that has conflicting formatting properties, the property will return a value that indicates that the selection uses multiple formatting options. For example, the SelectionFont property will return null if the selected text uses multiple fonts. The SelectionColor property returns Color.Empty when the selected text contains multiple colors.

Loading a RichTextBox Control from a File

When we worked with the TextBox control in Chapter 12, the Text property was used to fill the TextBox control with text. Although the RichTextBox control also exposes the Text property, it’s often more convenient to fill the control directly using the LoadFile method, which fills the control directly from a file or a stream.

There are three versions of the LoadFile method. The first version accepts a string that contains the path to the file to be loaded, as shown here:

richTextBox.LoadFile(path);

This version of the LoadFile method assumes that the file contains RTF text. If the file contains text in another format, an ArgumentException exception will be thrown.

The second version of the LoadFile method also accepts a string for the file path, as well as a value from the RichTextBoxStreamType enumeration that’s used to specify the type of file that’s being loaded, as shown here:

richTextBox.LoadFile(path, RichTextBoxStreamType.RichText);

Values from the RichTextBoxStreamType enumeration are listed in Table 15-3.

The third version of LoadFile accepts a stream reference instead of a file path, as shown here:

FileStream fs = new FileStream(path, FileMode.Open);
richTextBox.LoadFile(fs, RichTextBoxStreamType.RichText);
Saving the Contents of a RichTextBox Control

The contents of a RichTextBox control can be written to a file or a stream with the SaveFile method. As with the LoadFile method, there are three versions of SaveFile. The first version saves the contents of the control into an RTF file located at a location specified by the path, as shown here:

richTextBox.SaveFile(path);

The second version of the SaveFile method enables you to specify the file format, as shown in the following code, using values from the RichTextBoxStreamType enumeration. (See Table 15-3 earlier in this chapter.)

richTextBox.SaveFile(path, RichTextBoxStreamType.RichText);

The third version of SaveFile is used to write the contents of the RichTextBox control into a stream, as shown here:

FileStream fs = new FileStream(path, FileMode.Create);
richTextBox.SaveFile(fs, RichTextBoxStreamType.RichText);


Part III: Programming Windows Forms