Export (0) Print
Expand All

RichTextBox Class

Represents a Windows rich text box control.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)] 
public class RichTextBox : TextBoxBase
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */ 
public class RichTextBox extends TextBoxBase
ComVisibleAttribute(true) 
ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) 
public class RichTextBox extends TextBoxBase
Not applicable.

With the RichTextBox 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 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 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 RichTextBox 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 RichTextBox 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 RichTextBox 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 RichTextBox controls. However, the RichTextBox control does not have the same 64K character capacity limit of the TextBox control. The RichTextBox is typically used to provide text manipulation and display features similar to word processing applications such as Microsoft Word.

NoteNote:

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

The following code example creates a RichTextBox 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 void CreateMyRichTextBox()
{
    RichTextBox richTextBox1 = 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);

    this.Controls.Add(richTextBox1);
}


public void CreateMyRichTextBox()
{
    RichTextBox richTextBox1 = new RichTextBox();

    richTextBox1.set_Dock(DockStyle.Fill);
    richTextBox1.LoadFile("C:\\MyDocument.rtf");
    richTextBox1.Find("Text", RichTextBoxFinds.MatchCase);
    richTextBox1.set_SelectionFont(new Font
        ("Verdana", 12, FontStyle.Bold));
    richTextBox1.set_SelectionColor(Color.get_Red());
    richTextBox1.SaveFile("C:\\MyDocument.rtf",
        RichTextBoxStreamType.RichText);
    this.get_Controls().Add(richTextBox1);
} //CreateMyRichTextBox

System.Object
   System.MarshalByRefObject
     System.ComponentModel.Component
       System.Windows.Forms.Control
         System.Windows.Forms.TextBoxBase
          System.Windows.Forms.RichTextBox
             Microsoft.Ink.InkEdit

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft