System.Windows.Forms.RichTextBox Class

Represents a Windows rich text box control.

See Also: RichTextBox Members


[System.ComponentModel.Designer("System.Windows.Forms.Design.RichTextBoxDesigner, System.Design, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.ComponentModel.Design.IDesigner")]
public class RichTextBox : TextBoxBase


With the System.Windows.Forms.RichTextBox control, the user can enter and edit text. The control also provides more advanced formatting features than the standard System.Windows.Forms.TextBox control. Text can be assigned directly to the control, or can be loaded from a rich text format (RTF) or plain text file. The text within the control can be assigned character and paragraph formatting.

The System.Windows.Forms.RichTextBox control provides a number of properties you can use to apply formatting to any portion of text within the control. To change the formatting of text, it must first be selected. Only selected text can be assigned character and paragraph formatting. Once a setting has been made to a selected section of text, all text entered after the selection is also formatted with the same settings until a setting change is made or a different section of the control's document is selected. The RichTextBox.SelectionFont property enables you to make text bold or italic. You can also use this property to change the size and typeface of the text. The RichTextBox.SelectionColor property enables you to change the color of the text. To create bulleted lists you can use the RichTextBox.SelectionBullet property. You can also adjust paragraph formatting by setting the RichTextBox.SelectionIndent, RichTextBox.SelectionRightIndent, and RichTextBox.SelectionHangingIndent properties.

The System.Windows.Forms.RichTextBox control provides methods that provide functionality for opening and saving files. The RichTextBox.LoadFile(string) method enables you to load an existing RTF or ASCII text file into the control. You can also load data from an already opened data stream. The RichTextBox.SaveFile(string) enables you to save a file to RTF or ASCII text. Similar to the RichTextBox.LoadFile(string) method, you can also use the RichTextBox.SaveFile(string) method to save to an open data stream. The System.Windows.Forms.RichTextBox control also provides features for finding strings of text. The RichTextBox.Find(string) method is overloaded to find both strings of text as well as specific characters within the text of the control.

You can also initialize the System.Windows.Forms.RichTextBox control to data stored in memory. For example, you can initialize the RichTextBox.Rtf property to a string that contains the text to display, including the RTF codes that determine how the text should be formatted.

If the text within the control contains links, such as a link to a Web site, you can use the RichTextBox.DetectUrls property to display the link appropriately in the control's text. You can then handle the RichTextBox.LinkClicked event to perform the tasks associated with the link. The RichTextBox.SelectionProtected property enables you to protect text within the control from manipulation by the user. With protected text in your control, you can handle the RichTextBox.Protected event to determine when the user has attempted to modify protected text, and either alert the user that the text is protected or provide the user with a standard way to manipulate the protected text.

Applications that already use System.Windows.Forms.TextBox controls can easily be adapted to make use of System.Windows.Forms.RichTextBox controls. However, the System.Windows.Forms.RichTextBox control does not have the same 64K character capacity limit of the System.Windows.Forms.TextBox control. The System.Windows.Forms.RichTextBox is typically used to provide text manipulation and display features similar to word processing applications such as Microsoft Word.


The TextBoxBase.Undo method does not work with the Control.KeyPress or Control.TextChanged events.


Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
Assembly Versions: 1.0.5000.0,