Export (0) Print
Expand All
5 out of 16 rated this helpful - Rate this topic

RichTextBox Class

Represents a Windows rich text box control.

For a list of all members of this type, see RichTextBox Members.

System.Object
   System.MarshalByRefObject
      System.ComponentModel.Component
         System.Windows.Forms.Control
            System.Windows.Forms.TextBoxBase
               System.Windows.Forms.RichTextBox

[Visual Basic]
Public Class RichTextBox
   Inherits TextBoxBase
[C#]
public class RichTextBox : TextBoxBase
[C++]
public __gc class RichTextBox : public TextBoxBase
[JScript]
public class RichTextBox extends TextBoxBase

Thread Safety

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

Remarks

The RichTextBox control allows the user to enter and edit text while also providing 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 to either alert the user that the text is protected or to provide the user with a standard form of manipulating 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.

Example

[Visual Basic, C#, C++] The following 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 assumes 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 assumes that an RTF file is created, in the root of the C drive, containing the word "Text".

[Visual Basic] 
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


[C#] 
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);
}


[C++] 
public:
void CreateMyRichTextBox()
{
    RichTextBox* richTextBox1 = new RichTextBox();
    richTextBox1->Dock = DockStyle::Fill;


    richTextBox1->LoadFile(S"C:\\MyDocument.rtf");
    richTextBox1->Find(S"Text", RichTextBoxFinds::MatchCase);

    richTextBox1->SelectionFont = new System::Drawing::Font(S"Verdana", 12, FontStyle::Bold);
    richTextBox1->SelectionColor = Color::Red;

    richTextBox1->SaveFile(S"C:\\MyDocument.rtf", RichTextBoxStreamType::RichText);

    this->Controls->Add(richTextBox1);
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Windows.Forms

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System.Windows.Forms (in System.Windows.Forms.dll)

See Also

RichTextBox Members | System.Windows.Forms Namespace | TextBoxBase | TextBox

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.