Represents a Windows rich text box control.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
With the control, the user can enter and edit text. The control also provides more advanced formatting features than the standard 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 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 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 SelectionColor property enables you to change the color of the text. To create bulleted lists you can use the SelectionBullet property. You can also adjust paragraph formatting by setting the SelectionIndent, SelectionRightIndent, and SelectionHangingIndent properties.
The control provides methods that provide functionality for opening and saving files. The LoadFile 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 SaveFile enables you to save a file to RTF or ASCII text. Similar to the LoadFile method, you can also use the SaveFile method to save to an open data stream. The control also provides features for finding strings of text. The Find 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 control to data stored in memory. For example, you can initialize the 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 DetectUrls property to display the link appropriately in the control's text. You can then handle the LinkClicked event to perform the tasks associated with the link. The 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 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 TextBox controls can easily be adapted to make use of controls. However, the control does not have the same 64K character capacity limit of the TextBox control. The is typically used to provide text manipulation and display features similar to word processing applications such as Microsoft Word.
The following code example creates a control that loads an RTF file into the control and searches for the first instance of the word "Text." The code then changes the font style, font size, and font color of the selected text and saves the changes back to the original file. The example code finishes by adding the control to its Form. This example requires that the method created in the example code is added to a Form class and called from the constructor of the form. The example also requires that an RTF file is created, in the root of the C drive, containing the word "Text."
Public Sub CreateMyRichTextBox() Dim richTextBox1 As New RichTextBox() richTextBox1.Dock = DockStyle.Fill richTextBox1.LoadFile("C:\MyDocument.rtf") richTextBox1.Find("Text", RichTextBoxFinds.MatchCase) richTextBox1.SelectionFont = New Font("Verdana", 12, FontStyle.Bold) richTextBox1.SelectionColor = Color.Red richTextBox1.SaveFile("C:\MyDocument.rtf", RichTextBoxStreamType.RichText) Me.Controls.Add(richTextBox1) End Sub
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.